Android:连接到localhost上的服务器时设备上的连接超时

时间:2012-10-15 06:48:58

标签: java php android http android-networking

我无法弄清楚为什么会这样。我正在使用PHP服务器并将数据从Android应用程序发布到服务器。当我使用模拟器时,一切正常,但当我使用我的Android设备进行测试时,我收到连接超时错误。我正在使用我的服务器在Android设备上的相同网络。我在这里粘贴我的控制台日志,以便您可以清楚地了解这一点:

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)

这是我的JSON代码:

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

请帮助我克服这种奇怪的情况。

2 个答案:

答案 0 :(得分:0)

尝试增加连接超时...

HttpParameters = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(httpParameters, timeoutConnection  + 10000 );
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
httpclient = new DefaultHttpClient(httpParameters);

您的代码:

                String url = "http://10.0.2.2/insert_user_details.php";
            HttpPost httpPost = new HttpPost(url);
            HttpParams params = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(params, 15000);
            HttpClient client = new DefaultHttpClient(params);
            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();

答案 1 :(得分:0)

首先,您应该看到如何在Android设备上打开本地主机。 因为在您的Android设备上,您无法正常访问本地主机。