2012-11-04 17 views
0

每當我跑在我的MainActivity點擊列表項目啓動DetailsActivity即是假設diaply關於選擇列表中的一點信息項目(名稱,描述)的logcat引發以下我:NetworkOnMainThereadException約一個列表項顯示細節

E/AndroidRuntime(678):在 com.example.myfirstapp.JSONParser.makeHttpRequest(JSONParser.java:61) E/AndroidRuntime(678 ):在 com.example.myfirstapp.DetailsActivity $ GetProductDetails $ 1.run(Detail sActivity.java:93)

線61(JSONParser):HttpResponse httpResponse = httpClient.execute(httpGet); 線93(DetailsActivity):JSONObject json = jsonParser.makeHttpRequest( url_car_details, "GET", params);

DetailsActivity

package com.example.myfirstapp; 

/* 
* ...imports... 
*/ 

public class JSONParser { 

    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 

    // constructor 
    public JSONParser() { 

    } 

    // function get json from url 
    // by making HTTP POST or GET method 
    public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) { 

     // Making HTTP request 
     try { 

      // check for request method 
      if(method == "POST"){ 
       // request method is POST 
       // defaultHttpClient 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       HttpPost httpPost = new HttpPost(url); 
       httpPost.setEntity(new UrlEncodedFormEntity(params)); 

       HttpResponse httpResponse = httpClient.execute(httpPost); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       is = httpEntity.getContent(); 

      }else if(method == "GET"){ 
       // request method is GET 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       String paramString = URLEncodedUtils.format(params, "utf-8"); 
       url += "?" + paramString; 
       HttpGet httpGet = new HttpGet(url); 

       HttpResponse httpResponse = httpClient.execute(httpGet); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       is = httpEntity.getContent(); 
      }   

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // return JSON String 
     return jObj; 

    } 
} 

JSONParser

package com.example.myfirstapp; 

/* 
* ...imports... 
*/ 

public class DetailsActivity extends Activity { 

    TextView lblTitle; 
    TextView lblDesc; 

    String id; 

    // Progress Dialog 
    private ProgressDialog pDialog; 

    // JSON parser class 
    JSONParser jsonParser = new JSONParser(); 

    // single product url 
    private static final String url_car_details = "http://10.0.2.2/webservice/get_car_details.php"; 

    // JSON Node names 
    private static final String TAG_SUCCESS = "success"; 
    private static final String TAG_CAR = "car"; 
    private static final String TAG_ID = "id"; 
    private static final String TAG_NAME = "name"; 
    private static final String TAG_DESC = "description"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_details); 

     // getting product details from intent 
     Intent i = getIntent(); 

     // getting product id (pid) from intent 
     id = i.getStringExtra(TAG_ID); 

     // Getting complete product details in background thread 
     new GetProductDetails().execute(); 
    } 

    /** 
    * Background Async Task to Get complete product details 
    * */ 
    class GetProductDetails extends AsyncTask<String, String, String> { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(DetailsActivity.this); 
      pDialog.setMessage("Loading car details. Please wait..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

     /** 
     * Getting product details in background thread 
     * */ 
     protected String doInBackground(String... params) { 

      // updating UI from Background Thread 
      runOnUiThread(new Runnable() { 
       public void run() { 
        // Check for success tag 
        int success; 
        try { 
         // Building Parameters 
         List<NameValuePair> params = new ArrayList<NameValuePair>(); 
         params.add(new BasicNameValuePair("id", id)); 

         // getting product details by making HTTP request 
         // Note that product details url will use GET request 
         JSONObject json = jsonParser.makeHttpRequest(
           url_car_details, "GET", params); 

         // check your log for json response 
         Log.d("Single Car Details", json.toString()); 

         // json success tag 
         success = json.getInt(TAG_SUCCESS); 
         if (success == 1) { 
          // successfully received product details 
          JSONArray productObj = json 
            .getJSONArray(TAG_CAR); // JSON Array 

          // get first product object from JSON Array 
          JSONObject product = productObj.getJSONObject(0); 

          // product with this pid found 
          // Edit Text 
          lblTitle = (TextView) findViewById(R.id.lblTitle); 
          lblDesc = (TextView) findViewById(R.id.lblDesc); 

          // display product data in EditText 
          lblTitle.setText(product.getString(TAG_NAME)); 
          lblDesc.setText(product.getString(TAG_DESCRIPTION)); 

         }else{ 
          // product with pid not found 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 

      return null; 
     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 
      // dismiss the dialog once got all details 
      pDialog.dismiss(); 
     } 
    } 
} 
+0

那麼'runOnUiThread'的名字就是這麼做的,它在主UI線程上運行了一些代碼。所以你基本上在主UI線程上運行'doInBackground'方法中的所有代碼(這可能會阻塞UI線程)。 – Luksprog

回答

0

Chnage你的代碼爲:

/** 
    * Background Async Task to Get complete product details 
    * */ 
    class GetProductDetails extends AsyncTask<String, String, String> { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(DetailsActivity.this); 
      pDialog.setMessage("Loading car details. Please wait..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

     /** 
     * Getting product details in background thread 
     * */ 
     protected String doInBackground(String... params) { 


        // Check for success tag 
        int success; 
        try { 
         // Building Parameters 
         List<NameValuePair> params = new ArrayList<NameValuePair>(); 
         params.add(new BasicNameValuePair("id", id)); 

         // getting product details by making HTTP request 
         // Note that product details url will use GET request 
         JSONObject json = jsonParser.makeHttpRequest(
           url_car_details, "GET", params); 

         // check your log for json response 
         Log.d("Single Car Details", json.toString()); 

         // json success tag 
         success = json.getInt(TAG_SUCCESS); 
         if (success == 1) { 
          // successfully received product details 
          JSONArray productObj = json 
            .getJSONArray(TAG_CAR); // JSON Array 

          // get first product object from JSON Array 
          JSONObject product = productObj.getJSONObject(0); 


        // updating UI from Background Thread 
        DetailsActivity.this.runOnUiThread(new Runnable() { 
        @Override       
        public void run() { 
          // product with this pid found 
          // Edit Text 
          lblTitle = (TextView) findViewById(R.id.lblTitle); 
          lblDesc = (TextView) findViewById(R.id.lblDesc); 

          // display product data in EditText 
          lblTitle.setText(product.getString(TAG_NAME)); 
          lblDesc.setText(product.getString(TAG_DESCRIPTION)); 

         }else{ 
          // product with pid not found 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 

      return null; 
     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 
      // dismiss the dialog once got all details 
      pDialog.dismiss(); 
     } 
    } 
} 

第二種解決方案是從doInBackground刪除runOnUiThread,然後移動內部的AsyncTaskonPostExecute所有UI elememts。因爲onPostExecute時得到doInBackground完成給定的任務完全和你能夠訪問內部onPostExecute

所有UI元素稱爲
+0

他也在更新'doInBackground'中的textviews。 – dmon

+0

我不斷收到{和}符號的錯誤。我試圖弄清楚過去一小時有什麼問題。他們不匹配。 – user1701467

0

什麼Luksprog說,這是缺少了一點:

protected String doInBackground(String... params) { 
    // updating UI from Background Thread 
     runOnUiThread(new Runnable() { 

將需要更新的代碼UI到onPostExecute()。基本上將檢索到的數據保存在變量中並「返回」它。

相關問題