Android将HTTP连接到MVC Asp.net控制器

时间:2013-07-10 18:01:02

标签: android asp.net-mvc http

您好我正试图从我的MVC asp.net获取一个json对象。

我收到错误

            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;
    }

错误:

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;
        }

我目前通过localhost连接。

问题发生在这一行: HttpResponse r = client.execute(get);

它没有执行,VM上的应用程序崩溃了。

我修改了代码看起来像这样但仍然得到同样的错误:

   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;
        }

Log Cat给我这个错误: 07-10 20:53:23.578:E / ActivityThread(661):服务com.android.exchange.ExchangeService泄露了最初绑定的ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d17eb0 07-10 20:53:23.578:E / ActivityThread(661):android.app.ServiceConnectionLeaked:服务com.android.exchange.ExchangeService泄漏了最初绑定的ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d17eb0这里 07-10 20:53:23.578:E / ActivityThread(661):在android.app.LoadedApk $ ServiceDispatcher。(LoadedApk.java:969) 07-10 20:53:23.578:E / ActivityThread(661):在android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) 07-10 20:53:23.578:E / ActivityThread(661):在android.app.ContextImpl.bindService(ContextImpl.java:1418) 07-10 20:53:23.578:E / ActivityThread(661):在android.app.ContextImpl.bindService(ContextImpl.java:1407) 07-10 20:53:23.578:E / ActivityThread(661):在android.content.ContextWrapper.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):在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 / ActivityThread(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:服务com.android.exchange.ExchangeService泄露了最初绑定的ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d17eb0这里 07-10 20:53:23.588:E / StrictMode(661):在android.app.LoadedApk $ ServiceDispatcher。(LoadedApk.java:969) 07-10 20:53:23.588:E / StrictMode(661):在android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) 07-10 20:53:23.588:E / StrictMode(661):在android.app.ContextImpl.bindService(ContextImpl.java:1418) 07-10 20:53:23.588:E / StrictMode(661):在android.app.ContextImpl.bindService(ContextImpl.java:1407) 07-10 20:53:23.588:E / StrictMode(661):在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):在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):解绑失败:无法找到android.os.BinderProxy@4100de60的连接

1 个答案:

答案 0 :(得分:1)

如果没有更多信息,很难说,但您可能正在UI线程上运行此操作,这是不允许的。如果是这种情况,您将需要使用AsynTask。