gpt4 book ai didi

java - 为什么这会给出 NetworkOnMainThreadException?

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

我试图从 MySQL 数据库获取产品的详细信息,但是当我编译代码时,即使我在调用 GetProductDetails 类来执行时将其用作 AsyncTask,也会收到 NetworkOnMainThreadException 错误。我真的不明白为什么会发生这种情况,因为我是按照教程来做的,而且没有看到任何错误。

这是我正在使用的代码:

public class ListClubs extends Activity {

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clubs);
new GetProductDetails().execute();

(...some code...)
}

class GetProductDetails extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ListClubs.this);
pDialog.setMessage("Loading product 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("pid", Integer.toString(1)));

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

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

// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray productObj = json.getJSONArray(TAG_PRODUCT); // JSON Array

// get first product object from JSON Array
JSONObject product = productObj.getJSONObject(0);

}else{
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});

return null;
}

protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}

}

这是日志:

FATAL EXCEPTION: main
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.goout.JSONParser.makeHttpRequest(JSONParser.java:62)
at com.goout.ListClubs$GetProductDetails$1.run(ListClubs.java:464)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5227)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)

谢谢!

最佳答案

doInbackground() 中取出这些东西,看看会发生什么

 runOnUiThread(new Runnable() {
public void run() {

AsyncTask 的要点是在 doInBackground() 中运行网络内容或任何繁重的工作,然后使用其他方法来更新 UI 如果需要的话。因此,您不想做与其目的相反的事情,即使用 runOnUiThread() 告诉其后台代码 (doInbackground()) 在 UI Thread 上运行.

此外,really good information on AsyncTask Here

关于java - 为什么这会给出 NetworkOnMainThreadException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18390182/

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