We’ve created a program allowing connection from the android device to a server to send and return information. Running on Android 2.3.3 on the emulator, the app works fine.
Once we put it onto our Android 4.0.4 phone, the app crashed when trying to connect to the server.
We stripped the application down just to leave the connection code to try and debug and work out why it was crashing. Same story, on the V4 phone it connected on the V2 emulator it crashed.
We tried a V4.1 emulator and it crashed – yet on a version 2 Samsung tablet it worked.
When connecting to our server through the devices web broswer all versions were able to connect but not through the app.
Thanks for your help
package com.example.servertester;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private Context context = this;
EditText text, password;
Button button;
Toast message;
String proxyAddress, serverAddress;
int proxyPort;
private final HTTPCom HTTPC = new HTTPCom();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById (R.id.text);
password = (EditText) findViewById (R.id.password);
button = (Button) findViewById (R.id.button);
text.setText("");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String response = "";
JSONObject jobject = new JSONObject();
String request = jobject.toString();
response = HTTPC.HTTPJSONPost(context, "", "" + request, serverAddress, proxyAddress, proxyPort); //--- connection with proxy
//response = HTTPC.HTTPJSONPost(context, "", "" + request, serverAddress, null, null); //--- connection without proxy
message = Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG);
message.show();
}
});
}
}
Note: just for showing the code on here I have left the ip addresses undefined however in the orginal code they are defined.
We get the same result if we connect via a proxy or not.
09-11 08:50:33.083: D/AndroidRuntime(662): Shutting down VM
09-11 08:50:33.083: W/dalvikvm(662): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-11 08:50:33.123: E/AndroidRuntime(662): FATAL EXCEPTION: main
09-11 08:50:33.123: E/AndroidRuntime(662): android.os.NetworkOnMainThreadException
09-11 08:50:33.123: E/AndroidRuntime(662): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
09-11 08:50:33.123: E/AndroidRuntime(662): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
09-11 08:50:33.123: E/AndroidRuntime(662): at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
09-11 08:50:33.123: E/AndroidRuntime(662): at libcore.io.IoBridge.connect(IoBridge.java:112)
09-11 08:50:33.123: E/AndroidRuntime(662): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-11 08:50:33.123: E/AndroidRuntime(662): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
09-11 08:50:33.123: E/AndroidRuntime(662): at java.net.Socket.connect(Socket.java:842)
09-11 08:50:33.123: E/AndroidRuntime(662): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
09-11 08:50:33.123: E/AndroidRuntime(662): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
09-11 08:50:33.123: E/AndroidRuntime(662): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-11 08:50:33.123: E/AndroidRuntime(662): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-11 08:50:33.123: E/AndroidRuntime(662): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-11 08:50:33.123: E/AndroidRuntime(662): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-11 08:50:33.123: E/AndroidRuntime(662): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
09-11 08:50:33.123: E/AndroidRuntime(662): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
09-11 08:50:33.123: E/AndroidRuntime(662): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
09-11 08:50:33.123: E/AndroidRuntime(662): at com.example.servertester.HTTPCom.HTTPPost(HTTPCom.java:68)
09-11 08:50:33.123: E/AndroidRuntime(662): at com.example.servertester.HTTPCom.HTTPJSONPost(HTTPCom.java:27)
09-11 08:50:33.123: E/AndroidRuntime(662): at com.example.servertester.MainActivity$1.onClick(MainActivity.java:41)
09-11 08:50:33.123: E/AndroidRuntime(662): at android.view.View.performClick(View.java:4084)
09-11 08:50:33.123: E/AndroidRuntime(662): at android.view.View$PerformClick.run(View.java:16966)
09-11 08:50:33.123: E/AndroidRuntime(662): at android.os.Handler.handleCallback(Handler.java:615)
09-11 08:50:33.123: E/AndroidRuntime(662): at android.os.Handler.dispatchMessage(Handler.java:92)
09-11 08:50:33.123: E/AndroidRuntime(662): at android.os.Looper.loop(Looper.java:137)
09-11 08:50:33.123: E/AndroidRuntime(662): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-11 08:50:33.123: E/AndroidRuntime(662): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 08:50:33.123: E/AndroidRuntime(662): at java.lang.reflect.Method.invoke(Method.java:511)
09-11 08:50:33.123: E/AndroidRuntime(662): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-11 08:50:33.123: E/AndroidRuntime(662): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-11 08:50:33.123: E/AndroidRuntime(662): at dalvik.system.NativeStart.main(Native Method)
NetworkOnMainThreadException occurs when you any server related operation in Main Ui thread.
Move your server related code in AsyncTask or in Different Thread.
What Google Says
How to FIX : The problem here is simply that you need to make your web service calls (or what-have-you) on a separate thread.