android.os.NetworkOnMainThreadException - 我该如何解决这个问题?

时间:2013-05-17 10:35:07

标签: android

代码:

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部分,它会起作用,但对我来说没有运气......我怎么能解决这个问题?

1 个答案:

答案 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上执行 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)); } } } 。例如,您的AsyncTask应该如下所示:

{{1}}