2014-01-24 40 views
-1

我試着從一個php文件如何解決問題與mainthread

public void connect(){ 
    System.out.println("%%%%%%%%%%%%%%%%%1"); 
    Thread t = new Thread(){ 

     @Override 
     public void run() { 

    try { 

     System.out.println("%%%%%%%%%%%%%%%%%2"); 
     HttpParams params = new BasicHttpParams(); 
     HttpConnectionParams.setSoTimeout(params, 0); 
     HttpClient httpClient = new DefaultHttpClient(params); 
     String urlString = "http://url"; 
     //prepare the HTTP GET call 
     HttpGet httpget = new HttpGet(urlString); 
     //get the response entity 
     HttpEntity entity = httpClient.execute(httpget).getEntity(); 
     System.out.println("%%%%%%%%%%%%%%%%%3"); 
     if (entity != null) { 
      //get the response content as a string 
      String response = EntityUtils.toString(entity); 
      //consume the entity 
      entity.consumeContent(); 

      // When HttpClient instance is no longer needed, shut down the connection manager to ensure immediate deallocation of all system resources 
      httpClient.getConnectionManager().shutdown(); 

      //return the JSON response 

      JSONArray jsonarray = new JSONArray(response); 
      JSONObject jb =(JSONObject) jsonarray.getJSONObject(0); 
      String name= jb.getString("name"); 
      String punkt = jb.getString("punktezahl"); 

      //String name = jsonarray.getString("name"); 
      System.out.println("HEEEEEEEEEEEEEEEEEEEEEEEEEEEE" + name); 
      fuehrender.setText(name); 
      punkte.setText(punkt); 
       } 



    }catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

}; 
t.start(); 
} 

得到JSON數據如果我做這樣我得到的消息只有創建一個視圖層次可以觸摸其觀點原來的線程。

是因爲此錯誤消息我試着像這樣的:

public void connect(){ 
    System.out.println("%%%%%%%%%%%%%%%%%1"); 
    runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 

    try { 

     System.out.println("%%%%%%%%%%%%%%%%%2"); 
     HttpParams params = new BasicHttpParams(); 
     HttpConnectionParams.setSoTimeout(params, 0); 
     HttpClient httpClient = new DefaultHttpClient(params); 
     String urlString = "http://url"; 
     //prepare the HTTP GET call 
     HttpGet httpget = new HttpGet(urlString); 
     //get the response entity 
     HttpEntity entity = httpClient.execute(httpget).getEntity(); 
     System.out.println("%%%%%%%%%%%%%%%%%3"); 
     if (entity != null) { 
      //get the response content as a string 
      String response = EntityUtils.toString(entity); 
      //consume the entity 
      entity.consumeContent(); 

      // When HttpClient instance is no longer needed, shut down the connection manager to ensure immediate deallocation of all system resources 
      httpClient.getConnectionManager().shutdown(); 

      //return the JSON response 

      JSONArray jsonarray = new JSONArray(response); 
      JSONObject jb =(JSONObject) jsonarray.getJSONObject(0); 
      String name= jb.getString("name"); 
      String punkt = jb.getString("punktezahl"); 

      //String name = jsonarray.getString("name"); 
      System.out.println("HEEEEEEEEEEEEEEEEEEEEEEEEEEEE" + name); 
      fuehrender.setText(name); 
      punkte.setText(punkt); 
       } 



    }catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

}); 

} 
} 

現在,我得到NetworkOnMainThread錯誤信息。如何通過這個厄運循環打破?

+0

發生的AsyncTask您try-catch塊的。 –

+0

簡短的回答是:你不能在同一個線程上聯網**和** ui。 – njzk2

回答

1

您有runOnUiThread。去掉它。它只能用於更新ui而不是http請求。

使用AsyncTask是一個更好的選擇。在doInBackground中設置http獲取請求並解析響應。您可以將結果返回doInbackground,這是參數onPostExecute的參數。

因此,您可以更新在ui線程上調用的onPostExecute中的ui。

http://developer.android.com/reference/android/os/AsyncTask.html

例子:

要調用

new TheTask().execute(); // in ui thread 

AsyncTask一個內部類活動

class TheTask extends AsyncTask<Void,Void,String> 
{ 

    @Override 
    protected String doInBackground(Void... params1) { 
     String response = null; 
      try { 
       HttpParams params = new BasicHttpParams(); 
       HttpConnectionParams.setSoTimeout(params, 0); 
       HttpClient httpClient = new DefaultHttpClient(params); 
       String urlString = "http://url"; 
       HttpGet httpget = new HttpGet(urlString); 
       HttpEntity entity = httpClient.execute(httpget).getEntity(); 
       response = EntityUtils.toString(entity); 
       httpClient.getConnectionManager().shutdown(); 
       } 
       catch(Exception e) 
       { 
        e.printStackTrace(); 
       } 
     return response; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
     JSONArray jsonarray = new JSONArray(result; 
     JSONObject jb =(JSONObject) jsonarray.getJSONObject(0); 
     String name= jb.getString("name"); 
     String punkt = jb.getString("punktezahl"); 
     fuehrender.setText(name); 
     punkte.setText(punkt); 
    } 

} 
+0

如果我這樣做,我得到NetworkOnMainThread異常! – user896692

+0

@ user896692你需要使用線程或AsyncTask。 AsyncTask更容易。 – Raghunandan

+1

可能因爲你從'UI'調用'connect()'。從背景'Thread'調用它。但我同意,[AsyncTask](http://developer.android.com/reference/android/os/AsyncTask.html)更容易 – codeMagic