0

每当我运行并单击我的MainActivity中的列表项以启动DetailsActivity 时,这应该会显示有关所选列表项(名称,描述)的更多信息,LogCat 会向我抛出以下内容:

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

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

详情活动

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;

    }
}

JSON解析器

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();
        }
    }
}
4

2 回答 2

0

将您的代码更改为:

  /**
     * 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();
        }
    }
}

第二种解决 方案是runOnUiThread从. 因为在完成给定任务时会被调用,并且您可以访问 onPostExecute 中的所有 UI 元素doInBackgroundonPostExecuteAsyncTaskonPostExecutedoInBackground

于 2012-11-04T19:45:25.460 回答
0

Luksprog 所说的,这没有抓住重点:

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

将需要更新 UI 的代码移至onPostExecute(). 基本上将检索到的数据保存在一个变量中并“返回”它。

于 2012-11-04T19:50:14.647 回答