gpt4 book ai didi

java - 创建新线程的限制

转载 作者:行者123 更新时间:2023-12-01 06:39:38 25 4
gpt4 key购买 nike

我在主线程上不断收到网络错误。它指向代码中的这一部分:HttpResponse response = httpclient.execute(httppost);

这是我的整个线程,但我不明白它是如何说我在主线程上运行它的,而它绝对是在这个特定的单独线程上运行的!

new Thread(new Runnable() {

@Override
public void run() {

try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mywebsite.com");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", name));
nameValuePairs.add(new BasicNameValuePair("phonenumber", phone));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = httpclient.execute(httppost);

result = EntityUtils.toString(response.getEntity());
edit_name.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(), result, Toast.LENGTH_LONG).show();
}
});

db.submitFirstUser(name, phone);
}
catch(ClientProtocolException e){

}
catch (IOException e) {

}

}

}).run();

堆栈跟踪如下:

05-05 20:42:05.143: E/AndroidRuntime(31768): FATAL EXCEPTION: main
05-05 20:42:05.143: E/AndroidRuntime(31768): android.os.NetworkOnMainThreadException
05-05 20:42:05.143: E/AndroidRuntime(31768): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
05-05 20:42:05.143: E/AndroidRuntime(31768): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-05 20:42:05.143: E/AndroidRuntime(31768): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-05 20:42:05.143: E/AndroidRuntime(31768): at java.net.InetAddress.getAllByName(InetAddress.java:214)
05-05 20:42:05.143: E/AndroidRuntime(31768): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
05-05 20:42:05.143: E/AndroidRuntime(31768): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-05 20:42:05.143: E/AndroidRuntime(31768): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-05 20:42:05.143: E/AndroidRuntime(31768): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-05 20:42:05.143: E/AndroidRuntime(31768): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-05 20:42:05.143: E/AndroidRuntime(31768): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-05 20:42:05.143: E/AndroidRuntime(31768): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-05 20:42:05.143: E/AndroidRuntime(31768): at com.electricflurry.MyProfileFragment.doNetwork(MyProfileFragment.java:136)
05-05 20:42:05.143: E/AndroidRuntime(31768): at com.electricflurry.MyProfileFragment$1$1.run(MyProfileFragment.java:99)
05-05 20:42:05.143: E/AndroidRuntime(31768): at java.lang.Thread.run(Thread.java:856)
05-05 20:42:05.143: E/AndroidRuntime(31768): at com.electricflurry.MyProfileFragment$1.onClick(MyProfileFragment.java:101)
05-05 20:42:05.143: E/AndroidRuntime(31768): at android.view.View.performClick(View.java:4204)
05-05 20:42:05.143: E/AndroidRuntime(31768): at android.view.View$PerformClick.run(View.java:17355)
05-05 20:42:05.143: E/AndroidRuntime(31768): at android.os.Handler.handleCallback(Handler.java:725)
05-05 20:42:05.143: E/AndroidRuntime(31768): at android.os.Handler.dispatchMessage(Handler.java:92)
05-05 20:42:05.143: E/AndroidRuntime(31768): at android.os.Looper.loop(Looper.java:137)
05-05 20:42:05.143: E/AndroidRuntime(31768): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-05 20:42:05.143: E/AndroidRuntime(31768): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 20:42:05.143: E/AndroidRuntime(31768): at java.lang.reflect.Method.invoke(Method.java:511)
05-05 20:42:05.143: E/AndroidRuntime(31768): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-05 20:42:05.143: E/AndroidRuntime(31768): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-05 20:42:05.143: E/AndroidRuntime(31768): at dalvik.system.NativeStart.main(Native Method)
05-05 20:43:35.640: I/Process(31768): Sending signal. PID: 31768 SIG: 9

最佳答案

您实际上正在调用在主线程上执行代码的 run() 方法。调用 start() 方法将生成一个单独的线程。

关于java - 创建新线程的限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16390708/

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