gpt4 book ai didi

java - android.os.NetworkOnMainThreadException 的替代解决方案

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

现在我正在解决

android.os.NetworkOnMainThreadException

我将connect方法放入单独的线程中,但是当线程启动时,start()方法不会调用Run(),- 并且还使用 AsyncTask,任务不会调用 doInBackground() 方法!

<小时/>

//对于 AsyncTask,我的代码是

public class ConnectTask extends AsyncTask<URL, Integer, HttpEntity> {

HttpEntity entity;
String statue;
LogInJSONActivity mainActivity;
@Override
protected HttpEntity doInBackground(URL... arg0) {
// TODO Auto-generated method stub
mainActivity.setString("Inside Do in background");
entity = connect(arg0[0]);
mainActivity.setHttp(entity);
return entity;
}

public HttpEntity connect(String url) {

statue= "Inside Connect";
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
HttpEntity entity = null;
try {
response = httpclient.execute(httpget);
entity = response.getEntity();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return entity;
}
public void setActivity(LogInJSONActivity act){
mainActivity = act;
}
}

-> 在主要 Activity 中

URL url = null;
try {
url = new URL("http://www.flickr.com/photos/51469488@N03/");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ConnectTask backgroundTask = (ConnectTask) new ConnectTask();
backgroundTask.setActivity(this);
backgroundTask.execute(url);
<小时/>

//对于线程,我的代码是:-

public class ConnectThread extends Thread {

HttpEntity entity;
String statue;
LogInJSONActivity mainActivity;
@Override
public void run() {
// TODO Auto-generated method stub
mainActivity.setString("Inside Run");
entity = connect("http://www.flickr.com/photos/51469488@N03/");
mainActivity.setHttp(entity);
super.run();
}

public HttpEntity connect(String url) {

statue= "Inside Connect";
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
HttpEntity entity = null;
try {
response = httpclient.execute(httpget);
entity = response.getEntity();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(entity !=null)
System.out.print("Valid Entity");
else
System.out.print("Null Entity");


return entity;
}
public void setActivity(LogInJSONActivity act){
mainActivity = act;
}
}
<小时/>

-> 在 MainActivity 中

ConnectThread t = new ConnectThread();
t.setActivity(this);
t.start();

最佳答案

您的错误可能是因为您正在UI线程上执行一些网络操作,这不是从API 11允许。我想说不要创建单独的Thread,只需使用AsyncTask类来执行您的Network > 操作。只需在 doInBackground() 中执行网络操作,然后从 UI 线程调用 AsyncTask 类,

new YourAsyncTask().execute(url);

这一切都不需要通过创建单独的 Thread 类并使其变得更加复杂来付出太多努力。

关于java - android.os.NetworkOnMainThreadException 的替代解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9249129/

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