0

I keep getting an error with network on main thread. It points to this part in the code: HttpResponse response = httpclient.execute(httppost);

This is my whole thread, but I don't understand how it says I am running it on the main thread when its most definitely on this specific separate thread!

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();

Stack trace is below:

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

4 回答 4

4

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

于 2013-05-06T01:03:36.190 回答
3

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

于 2013-05-06T01:03:17.450 回答
2

要启动一个线程使用start(),所以:

.run();

应该:

.start();
于 2013-05-06T01:03:18.897 回答
2

你应该开始线程

thread.start();

不与thread.run()

于 2013-05-06T01:03:02.143 回答