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