2013-05-17 29 views
-3

代碼:android.os.NetworkOnMainThreadException - 我應該如何解決這個問題?

public class OfferDetails extends Activity { 

    TextView name_offerdetails; 
    TextView start_offerdetails; 
    TextView end_offerdetails; 
    TextView description_offerdetails; 

    String offer_id; 

    String message; 
    int success; 

    private ProgressDialog pDialog; 

    JSONParser jsonParser = new JSONParser(); 

    //single product url 
    public static final String DOMAIN = "192.168.0.112"; 
    private static String url_all_offers = "http://" + DOMAIN + "/iChop/get_offer_details.php"; 

    //JSON Node names 
    private static final String TAG_SUCCESS = "success"; 
    //private static final String TAG_MESSAGE = "message"; 
    private static final String TAG_OFFER = "offer"; 
    private static final String TAG_OFFER_ID = "offer_id"; 
    private static final String TAG_NAME = "m_name"; 
    private static final String TAG_START = "start_date"; 
    private static final String TAG_END = "end_date"; 
    private static final String TAG_DESCRIPTION = "o_description"; 

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

     //Getting offer details from intent 
     Intent i = getIntent(); 

     offer_id = i.getStringExtra(TAG_OFFER_ID); 

     new GetOfferDetails().execute(); 
    } 

    class GetOfferDetails extends AsyncTask<String, String, String>{ 
     @Override 
     protected void onPreExecute(){ 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(OfferDetails.this); 
      pDialog.setMessage("Loading offer details. Please wait..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

     protected String doInBackground(String... params){ 
      runOnUiThread(new Runnable(){ 
       public void run(){ 
        try{ 
         List<NameValuePair> params = new ArrayList<NameValuePair>(); 
         params.add(new BasicNameValuePair("offer_id", offer_id)); 

         JSONObject json = jsonParser.makeHttpRequest(url_all_offers, "GET", params); 

         Log.d("Single Offer details", json.toString()); 

         success = json.getInt(TAG_SUCCESS); 
         if(success == 1){ 
          JSONArray offerObj = json.getJSONArray(TAG_OFFER); 

          //get first offer objects from JSON array 
          JSONObject offer = offerObj.getJSONObject(0); 

          //offer with this offer_id found 
          name_offerdetails = (TextView) findViewById(R.id.name_offerdetails); 
          start_offerdetails = (TextView) findViewById(R.id.start_offerdetails); 
          end_offerdetails = (TextView) findViewById(R.id.end_offerdetails); 
          description_offerdetails = (TextView) findViewById(R.id.description_offerdetails); 

          //display in text view 
          name_offerdetails.setText(offer.getString(TAG_NAME)); 
          start_offerdetails.setText(offer.getString(TAG_START)); 
          end_offerdetails.setText(offer.getString(TAG_END)); 
          description_offerdetails.setText(offer.getString(TAG_DESCRIPTION)); 
         } else { 
          // 
         } 
        } catch (JSONException e){ 
         e.printStackTrace(); 
        } 
       } 
      }); 

      return null; 
     } 

     protected void onPostExecute(String file_url){ 
      pDialog.dismiss(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.offer_details, menu); 
     return true; 
    } 

    public void Code (View view) { 
     Intent code = new Intent (this, Code.class); 
     startActivity(code); 
    } 

    public void Home (View view) { 
     Intent home = new Intent (this, Home.class); 
     startActivity(home); 
    } 

    public void Setting (View view) { 
     Intent setting = new Intent (this, Setting.class); 
     startActivity(setting); 
    } 

} 

我也跟着上AndroidHive的教程,我得到這個錯誤。

05-17 18:16:05.905: E/AndroidRuntime(4858): android.os.NetworkOnMainThreadException 

我在網上查,有的說如果我移動runOnUiThread下降到剛剛的setText部分,將工作,但沒有運氣,我......我怎麼能解決這個問題?

+0

您可以通過在主線程上不使用網絡來解決此問題。這是obvios – Greensy

+0

這並沒有幫助:/ –

回答

0

你必須在runOnUiThread裏面移動只有代碼需要被執行的代碼UI Thread並不是所有的doInBackground的主體。在您的代碼中只有

name_offerdetails = (TextView) findViewById(R.id.name_offerdetails); 
start_offerdetails = (TextView) findViewById(R.id.start_offerdetails); 
end_offerdetails = (TextView) findViewById(R.id.end_offerdetails); 
    description_offerdetails = (TextView) findViewById(R.id.description_offerdetails); 

         //display in text view 
    name_offerdetails.setText(offer.getString(TAG_NAME)); 
    start_offerdetails.setText(offer.getString(TAG_START)); 
    end_offerdetails.setText(offer.getString(TAG_END)); 
    description_offerdetails.setText(offer.getString(TAG_DESCRIPTION)); 

需要在UI線程上運行。另外,你可以讓doInBackground返回JSONObject。並在onPostExecute中運行需要在UI線程上執行的代碼。實際上onPostExecute總是在UI Thread上被執行。例如,您的AsyncTask應該如下所示:

class GetOfferDetails extends AsyncTask<String, String, JSONObject>{ 
    @Override 
    protected void onPreExecute(){ 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(OfferDetails.this); 
     pDialog.setMessage("Loading offer details. Please wait..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    protected JSONObject doInBackground(String... params){ 

       try{ 
        List<NameValuePair> params = new ArrayList<NameValuePair>(); 
        params.add(new BasicNameValuePair("offer_id", offer_id)); 

        JSONObject json = jsonParser.makeHttpRequest(url_all_offers, "GET", params); 

        Log.d("Single Offer details", json.toString()); 

        success = json.getInt(TAG_SUCCESS); 
        if(success == 1){ 
         JSONArray offerObj = json.getJSONArray(TAG_OFFER); 

         //get first offer objects from JSON array 
         JSONObject offer = offerObj.getJSONObject(0); 

         return offer; 
        } else { 
         // 
        } 
       } catch (JSONException e){ 
        e.printStackTrace(); 
       } 


     return null; 
    } 

    protected void onPostExecute(JSONObject offer){ 
     pDialog.dismiss(); 
     if (offer != null) { 
      //offer with this offer_id found 
      name_offerdetails = (TextView) findViewById(R.id.name_offerdetails); 
      start_offerdetails = (TextView) findViewById(R.id.start_offerdetails); 
      end_offerdetails = (TextView) findViewById(R.id.end_offerdetails); 
      description_offerdetails = (TextView) findViewById(R.id.description_offerdetails); 

         //display in text view 
      name_offerdetails.setText(offer.getString(TAG_NAME)); 
      start_offerdetails.setText(offer.getString(TAG_START)); 
      end_offerdetails.setText(offer.getString(TAG_END)); 
      description_offerdetails.setText(offer.getString(TAG_DESCRIPTION)); 
    } 
    } 
} 
+0

嘗試過你的方式......但不工作太 –

+0

什麼是「不工作太」是什麼意思?你得到相同的excption或什麼? – Blackbelt

+0

方式太多的錯誤..無法啓動應用程序..lur –