android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)

时间:2014-10-04 03:18:24

标签: java android json exception

我有一个JSON功能在不同的活动中使用相同的格式正常工作,但是当我在这里运行时我得到错误。我已经确定了实际的JSON调用,它抛出异常但不明白为什么。 请参阅' JSONObject json = jsonParser.makeHttpRequest(url_challenge_details," GET",params);'

请帮忙!如果您需要更多代码或日志,请告诉我。谢谢

    /**
 * Background Async Task to Get complete Challenge details
 * */
class GetChallengeDetails extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(EditChallengeActivity.this);
        pDialog.setMessage("Loading Challenge details. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Getting Challenge 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("pid", pid));

                    // getting Challenge details by making HTTP request
                    // Note that Challenge details url will use GET request
                    JSONObject json = jsonParser.makeHttpRequest(
                            url_challenge_details, "GET", params);

                    // check your log for json response
                    Log.d("Single Challenge Details", json.toString());

                    // json success tag
                    success = json.getInt(TAG_SUCCESS);
                    if (success == 1) {
                        // successfully received Challengedetails
                        JSONArray challengeObj = json
                                .getJSONArray(TAG_CHALLENGE); // JSON Array

                        // get first Challenge object from JSON Array
                        JSONObject challenge = challengeObj.getJSONObject(0);

                        // Challenge with this pid found
                        // Edit Text
                        txtName = (EditText) findViewById(R.id.inputName);
                        txtPrice = (EditText) findViewById(R.id.inputPrice);
                        txtDesc = (EditText) findViewById(R.id.inputDesc);

                        // display Challenge data in EditText
                        txtName.setText(challenge.getString(TAG_NAME));
                        txtPrice.setText(challenge.getString(TAG_PRICE));
                        txtDesc.setText(challenge.getString(TAG_DESCRIPTION));

                    }else{
                        // Challenge 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();
    }
}

以下是例外情况。

10-03 22:56:31.271: W/dalvikvm(3871): threadid=1: thread exiting with uncaught exception (group=0xb2ac1ba8)
10-03 22:56:31.461: E/AndroidRuntime(3871): FATAL EXCEPTION: main
10-03 22:56:31.461: E/AndroidRuntime(3871): Process: com.xxx.xxx, PID: 3871
10-03 22:56:31.461: E/AndroidRuntime(3871): android.os.NetworkOnMainThreadException
10-03 22:56:31.461: E/AndroidRuntime(3871):     at  android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
10-03 22:56:31.461: E/AndroidRuntime(3871):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
10-03 22:56:31.461: E/AndroidRuntime(3871):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-03 22:56:31.461: E/AndroidRuntime(3871):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-03 22:56:31.461: E/AndroidRuntime(3871):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
10-03 22:56:31.461: E/AndroidRuntime(3871):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-03 22:56:31.461: E/AndroidRuntime(3871):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
 10-03 22:56:31.461: E/AndroidRuntime(3871):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)

1 个答案:

答案 0 :(得分:1)

您的doInBackground调用runOnUiThread,有效地将操作从后台线程推回到UI线程。删除它并将您的UI更新代码移至onPostExecute