Android connecting HTTP to MVC Asp.net Controller
Hi i'm trying to get a json object from my MVC asp.net.
I'm getting an error
StringBuilder url = new StringBuilder(URL);
HttpGet get = new HttpGet (url.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
if(status == 200)
{
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONArray timeline = new JSONArray(data);
JSONObject last = timeline.getJSONObject(0);
return last;
}
else
{
Toast.makeText(Authenticate.this, "error", Toast.LENGTH_LONG);
return null;
}
The error:
Java.IOException.
public class Read extends AsyncTask <String,Integer,String>
{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try{
json = Verify();
return json.getString(params[0]);
}
catch (ClientProtocolException e)
{
}
catch (IOException e)
{
}
catch (JSONException e)
{
}
return null;
}
I am currently connecting via localhost.
The problem happen at the line: HttpResponse r = client.execute(get);
Its not executing and the application on the VM is crashing.
I have amended the code to look like this but still getting the same error:
public JSONObject Verify(HttpResponse r) throws ClientProtocolException, IOException, JSONException
{
//StringBuilder url = new StringBuilder(URL);
//Adds the pin and IMEI.
//url.append(b);
//HttpGet get = new HttpGet (url.toString());
//HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
if(status == 200)
{
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONArray timeline = new JSONArray(data);
JSONObject last = timeline.getJSONObject(0);
return last;
}
else
{
Toast.makeText(Authenticate.this, "error", Toast.LENGTH_LONG);
return null;
}
}
public class Read extends AsyncTask <String,Integer,String>
{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try{
HttpResponse r = client.execute(get);
json = Verify(r);
return json.getString(params[0]);
}
catch (ClientProtocolException e)
{
}
catch (IOException e)
{
}
catch (JSONException e)
{
}
return null;
}
The Log Cat is giving me this error: 07-10 20:53:23.578: E/ActivityThread(661): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d17eb0 that was originally bound here 07-10 20:53:23.578: E/ActivityThread(661): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d17eb0 that was originally bound here 07-10 20:53:23.578: E/ActivityThread(661): at android.app.LoadedApk$ServiceDispatcher.(LoadedApk.java:969) 07-10 20:53:23.578: E/ActivityThread(661): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) 07-10 20:53:23.578: E/ActivityThread(661): at android.app.ContextImpl.bindService(ContextImpl.java:1418) 07-10 20:53:23.578: E/ActivityThread(661): at android.app.ContextImpl.bindService(ContextImpl.java:1407) 07-10 20:53:23.578: E/ActivityThread(661): at android.content.Co ntextWrapper.bindService(ContextWrapper.java:473) 07-10 20:53:23.578: E/ActivityThread(661): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) 07-10 20:53:23.578: E/ActivityThread(661): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145) 07-10 20:53:23.578: E/ActivityThread(661): at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191) 07-10 20:53:23.578: E/ActivityThread(661): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850) 07-10 20:53:23.578: E/ActivityThread(661): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551) 07-10 20:53:23.578: E/ActivityThread(661): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549) 07-10 20:53:23.578: E/ActivityThread(661): at android.os.AsyncTask$2.call(AsyncTask.java:287) 07-10 20:53:23.578: E/ActivityThread(661): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 07-10 20:53:23.578: E/ActivityTh read(661): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 07-10 20:53:23.578: E/ActivityThread(661): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 07-10 20:53:23.578: E/ActivityThread(661): at java.lang.Thread.run(Thread.java:856) 07-10 20:53:23.588: E/StrictMode(661): null 07-10 20:53:23.588: E/StrictMode(661): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d17eb0 that was originally bound here 07-10 20:53:23.588: E/StrictMode(661): at android.app.LoadedApk$ServiceDispatcher.(LoadedApk.java:969) 07-10 20:53:23.588: E/StrictMode(661): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) 07-10 20:53:23.588: E/StrictMode(661): at android.app.ContextImpl.bindService(ContextImpl.java:1418) 07-10 20:53:23.588: E/StrictMode(661): at android.app.ContextImpl.bindService(ContextImpl.ja va:1407) 07-10 20:53:23.588: E/StrictMode(661): at android.content.ContextWrapper.bindService(ContextWrapper.java:473) 07-10 20:53:23.588: E/StrictMode(661): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) 07-10 20:53:23.588: E/StrictMode(661): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145) 07-10 20:53:23.588: E/StrictMode(661): at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191) 07-10 20:53:23.588: E/StrictMode(661): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850) 07-10 20:53:23.588: E/StrictMode(661): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551) 07-10 20:53:23.588: E/StrictMode(661): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549) 07-10 20:53:23.588: E/StrictMode(661): at android.os.AsyncTask$2.call(AsyncTask.java:287) 07-10 20:53:23.588: E/StrictMode(661): at java.util.concurrent.FutureTask.run(FutureTask.java: 234) 07-10 20:53:23.588: E/StrictMode(661): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 07-10 20:53:23.588: E/StrictMode(661): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 07-10 20:53:23.588: E/StrictMode(661): at java.lang.Thread.run(Thread.java:856) 07-10 20:53:23.588: W/ActivityManager(291): Unbind failed: could not find connection for android.os.BinderProxy@4100de60
It's hard to tell without more information, but you may be running this on your UI thread which is not allowed. If this is the case you will need to use an AsynTask.
链接地址: http://www.djcxy.com/p/80012.html