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;

    }
}

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

2 に答える 2

0

Luksprog が言ったこと、これは要点が欠けています:

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

UI を更新する必要があるコードを に移動しますonPostExecute()。基本的には取得したデータを変数に保存して「返す」。

于 2012-11-04T19:50:14.647 に答える
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();
        }
    }
}

2 番目の解決策は、すべての Ui 要素をから削除 runOnUiThreadして移動することです。特定のタスクが完全に完了すると呼び出され、onPostExecute 内のすべての UI 要素にアクセスできるためです。doInBackgroundonPostExecuteAsyncTaskonPostExecutedoInBackground

于 2012-11-04T19:45:25.460 に答える