-3

代码:

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 部分,它会起作用,但我没有运气......我该如何解决这个问题?

4

1 回答 1

0

您必须在并非所有的 doInBackground 主体上移动runOnUiThread唯一需要执行的代码。UI Thread仅在您的代码中

 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));
     }
    }
}
于 2013-05-17T10:36:38.707 回答