2013-07-10 77 views
0

嗨,我試圖從我的MVC asp.net得到一個JSON對象。Android連接HTTP到MVC Asp.net控制器

我得到一個錯誤

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

我目前通過本地主機連接。

問題發生在該行: 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; 
     } 

日誌貓給我這個錯誤: 07-10 20:53:23.578:E/ActivityThread( 661):Service com.android.exchange.ExchangeService泄露了最初綁定在此的ServiceConnection [email protected]17eb0 07-10 20:53:23.578:E/ActivityThread(661):android .app.ServiceConnectionLeaked:服務com.android.exchange.ExchangeService泄露了最初綁定在這裏的ServiceConnection [email protected]17eb0 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)在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):在android.app.ContextImpl.bindService(ContextImpl.java:1407) 07-10 20:53:23.578:E/ActivityThread(661):at 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):at android.os.AsyncTask $ 2.call(AsyncTask.java:287) 07-10 20:53:23.578:E/ActivityThread(661):在java.util。併發.FutureTask.run(FutureTask.java:234) 07-10 20:53:23.578:E/ActivityThread(661):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoo EExplorer.java:1080) 07-10 20:53:23.578:E/ActivityThread(661):在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):at 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):at android.app.ContextImpl.bindService (ContextImpl.java:1418) 07-10 20:53:23.588:E/StrictMode(661):at android.app.ContextImpl.bindService(ContextImpl.java: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):在com.android.emailcommon.ser (ServiceProxy.java:145) 07-10 20:53:23.588:E/StrictMode -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):在java.util.concurrent.FutureTask.run(FutureTask.java:234) 07-10 20:53:23.588:E/StrictMode(661):在java.util.concurrent.ThreadPoolExec utor.runWorker(ThreadPoolExecutor.java:1080) 07-10 20:53:23.588:E/StrictMode(661):在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573) 07-10 20:53:23.588:E/StrictMode(661):在java.lang.Thread.run(Thread.java:856) 07-10 20:53:23.588:W/ActivityManager(291):Unbind failed:could not找到連接[email protected]

回答

1

沒有更多的信息很難說,但你可能會在你的UI線程上運行這是不允許的。如果是這種情況,您將需要使用AsynTask。

+0

我在AsynTask運行它,如下所示 公共類閱讀擴展的AsyncTask <字符串,整數,字符串> \t \t { \t \t \t @覆蓋 \t \t \t保護字符串doInBackground(字符串... PARAMS ){ \t \t \t \t // TODO自動生成方法存根 \t \t \t \t嘗試{ \t \t \t \t \t json = Verify(); \t \t \t \t \t return json。的getString(PARAMS [0]); \t \t \t \t} \t \t \t \t趕上(ClientProtocolException E) \t \t \t \t \t {\t \t \t \t \t \t \t} \t \t \t \t趕上(IOException的E) \t \t \t \t \t {\t \t \t \t \t \t \t} \t \t \t \t趕上(JSONException E) \t \t \t \t { \t \t \t \t \t \t \t \t} \t \t \t \t return null; \t \t \t} – CRL88

+0

你可以編輯你最初的問題來添加那些東西嗎?這有點難以閱讀。 – user2483079

+0

我剛加入它感謝您的關注。 – CRL88