Android 无法连接网络服务器
Android can't connect webserver
我使用 "xampp" 构建了一个网络服务器,并在 C:\xampp\htdocs.
上创建了 "abc.php"
完成所有工作后,我link "localhost/abc.php"。
使用Pcgoogle欺骗URL可以显示"This is server's message".
到目前为止 good.Until 我想 android 连接网络服务器。
我尝试了很多方法(包括关闭防火墙或杀毒软件等),但仍然失败。
我已经尝试 google(android phone) 连接“http://MyIP/abc.php”,而不是通过这个程序。
结果是可以在google(android phone).
中显示我的"abc.php"
但不知道哪里出错了。
我无法使用我的程序获取 URL
package com.testdb;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView1);
db message = new db();
String msg = message.stringQuery("http://10.0.2.2/abc.php");
textView.setText("Server message is "+msg);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
-
package com.testdb;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class db {
public String stringQuery(String url){
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost method = new HttpPost(url);
HttpResponse response = httpclient.execute(method);
HttpEntity entity = response.getEntity();
if(entity != null){
return EntityUtils.toString(entity);
}
else{
return "No string.";
}
}
catch(Exception e){
return "Network problem";
}
}
}
这是我的日志
04-04 16:30:07.425: W/Trace(1183): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:16.906: E/Trace(1301): error opening trace file: No such file or directory (2)
04-04 16:30:16.906: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:16.916: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:16.916: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:17.006: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:17.006: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:17.796: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:17.796: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:17.816: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:17.836: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.046: D/libEGL(1301): loaded /system/lib/egl/libEGL_emulation.so
04-04 16:30:18.066: D/(1301): HostConnection::get() New Host Connection established 0x2a0ac400, tid 1301
04-04 16:30:18.106: D/libEGL(1301): loaded /system/lib/egl/libGLESv1_CM_emulation.so
04-04 16:30:18.126: D/libEGL(1301): loaded /system/lib/egl/libGLESv2_emulation.so
04-04 16:30:18.289: W/EGL_emulation(1301): eglSurfaceAttrib not implemented
04-04 16:30:18.309: D/OpenGLRenderer(1301): Enabling debug mode 0
04-04 16:30:18.309: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.316: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.316: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.376: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.386: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.386: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.386: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.386: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.386: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.396: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.486: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.496: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.496: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.496: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.689: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.716: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.786: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
10.0.2.2 是您本地网络上的 IP 地址。它无法从 Internet 访问,因此使用蜂窝网络连接的 Android phone 无法连接到它。您需要 运行 您的 Web 应用程序在 public 托管服务上,而不是在您的本地计算机上。
我使用 "xampp" 构建了一个网络服务器,并在 C:\xampp\htdocs.
上创建了 "abc.php"完成所有工作后,我link "localhost/abc.php"。
使用Pcgoogle欺骗URL可以显示"This is server's message".
到目前为止 good.Until 我想 android 连接网络服务器。
我尝试了很多方法(包括关闭防火墙或杀毒软件等),但仍然失败。
我已经尝试 google(android phone) 连接“http://MyIP/abc.php”,而不是通过这个程序。
结果是可以在google(android phone).
中显示我的"abc.php"但不知道哪里出错了。 我无法使用我的程序获取 URL
package com.testdb;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView1);
db message = new db();
String msg = message.stringQuery("http://10.0.2.2/abc.php");
textView.setText("Server message is "+msg);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
-
package com.testdb;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class db {
public String stringQuery(String url){
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost method = new HttpPost(url);
HttpResponse response = httpclient.execute(method);
HttpEntity entity = response.getEntity();
if(entity != null){
return EntityUtils.toString(entity);
}
else{
return "No string.";
}
}
catch(Exception e){
return "Network problem";
}
}
}
这是我的日志
04-04 16:30:07.425: W/Trace(1183): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:16.906: E/Trace(1301): error opening trace file: No such file or directory (2)
04-04 16:30:16.906: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:16.916: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:16.916: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:17.006: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:17.006: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:17.796: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:17.796: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:17.816: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:17.836: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.046: D/libEGL(1301): loaded /system/lib/egl/libEGL_emulation.so
04-04 16:30:18.066: D/(1301): HostConnection::get() New Host Connection established 0x2a0ac400, tid 1301
04-04 16:30:18.106: D/libEGL(1301): loaded /system/lib/egl/libGLESv1_CM_emulation.so
04-04 16:30:18.126: D/libEGL(1301): loaded /system/lib/egl/libGLESv2_emulation.so
04-04 16:30:18.289: W/EGL_emulation(1301): eglSurfaceAttrib not implemented
04-04 16:30:18.309: D/OpenGLRenderer(1301): Enabling debug mode 0
04-04 16:30:18.309: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.316: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.316: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.376: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.386: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.386: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.386: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.386: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.386: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.396: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.486: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.496: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.496: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.496: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.689: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.716: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.786: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
10.0.2.2 是您本地网络上的 IP 地址。它无法从 Internet 访问,因此使用蜂窝网络连接的 Android phone 无法连接到它。您需要 运行 您的 Web 应用程序在 public 托管服务上,而不是在您的本地计算机上。