Search code examples
javaphpandroidhttpandroid-networking

Android: Connection timeout on device while connecting to the server on localhost


I could not figure out why this is happening. I'm using PHP server and posting data from Android application to the server. When I'm using emulator all things are working fine but when I'm using my Android device for testing I'm getting Connection Timeout error. I'm using the same same network on which my server is on android device. I'm pasting my console log here so that, you can get a clear idea regarding this:

10-15 12:09:25.593: W/System.err(24334): org.apache.http.conn.ConnectTimeoutException: Connect to /10.0.2.2:80 timed out
10-15 12:09:25.609: W/System.err(24334):    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
10-15 12:09:25.609: W/System.err(24334):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:156)
10-15 12:09:25.609: W/System.err(24334):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-15 12:09:25.609: W/System.err(24334):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-15 12:09:25.609: W/System.err(24334):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
10-15 12:09:25.609: W/System.err(24334):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-15 12:09:25.609: W/System.err(24334):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-15 12:09:25.609: W/System.err(24334):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-15 12:09:25.609: W/System.err(24334):    at com.example.insertuserdetails.MainActivity$1.onClick(MainActivity.java:78)
10-15 12:09:25.609: W/System.err(24334):    at android.view.View.performClick(View.java:2538)
10-15 12:09:25.609: W/System.err(24334):    at android.view.View$PerformClick.run(View.java:9152)
10-15 12:09:25.609: W/System.err(24334):    at android.os.Handler.handleCallback(Handler.java:587)
10-15 12:09:25.609: W/System.err(24334):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 12:09:25.609: W/System.err(24334):    at android.os.Looper.loop(Looper.java:130)
10-15 12:09:25.617: W/System.err(24334):    at android.app.ActivityThread.main(ActivityThread.java:3689)
10-15 12:09:25.617: W/System.err(24334):    at java.lang.reflect.Method.invokeNative(Native Method)
10-15 12:09:25.617: W/System.err(24334):    at java.lang.reflect.Method.invoke(Method.java:507)
10-15 12:09:25.617: W/System.err(24334):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
10-15 12:09:25.617: W/System.err(24334):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-15 12:09:25.617: W/System.err(24334):    at dalvik.system.NativeStart.main(Native Method)

Here is my JSON code :

String url = "http://10.0.2.2/insert_user_details.php";
                HttpClient client = new DefaultHttpClient();
                HttpParams params = client.getParams();
                HttpConnectionParams.setConnectionTimeout(params, 15000);
                HttpPost httpPost = new HttpPost(url);
                httpPost.setHeader("json", jsonObject.toString());
                StringEntity se = null;
                se = new StringEntity(jsonObject.toString());
                se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                        "application/json"));
                httpPost.setEntity(se);
                HttpResponse response = client.execute(httpPost);
                int i = response.getStatusLine().getStatusCode();
                Toast.makeText(getBaseContext(), "Status: " + i,
                        Toast.LENGTH_LONG).show();

Please help me overcoming this weird situation.


Solution

  • First of all you should see that how to open local host on your android device. Because on your android device you can't access your local host normally.