gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:27:51 26 4
gpt4 key购买 nike

我有一个 JSON 函数在不同的 Activity 中以相同的格式正常工作,但是当我在这里运行它时,我收到错误。我已经确定了实际的 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();
}
}

以下是异常(exception)情况。

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)

最佳答案

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

关于java - android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java :1145),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26189274/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com