我不知道为什么会这样。我正在使用 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();
请帮助我克服这种奇怪的情况。