创建新线程的限制

时间:2013-05-06 01:00:17

标签: java android

我一直在主线程上遇到网络错误。它在代码中指向此部分: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

4 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:3)

run()只需调用Runnable上的run()方法即可。您希望start()在新线程上实际启动Runnable。

答案 2 :(得分:2)

您应该使用

启动线程
thread.start();

不是thread.run()

答案 3 :(得分:2)

要开始使用start()线程,请:

.run();

应该是:

.start();