我有这样的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
InetAddress ip;
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
String ipv4add;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
ip = InetAddress.getLocalHost();
ipv4add = ip.getHostAddress().toString();
System.out.println(ipv4add);
mWebview .loadUrl(ipv4add+"/Lab4/Task1/index.php");
mWebview.getSettings().setLoadsImagesAutomatically(true);
mWebview.getSettings().setJavaScriptEnabled(true);
mWebview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
setContentView(mWebview );
} catch (UnknownHostException e) {
e.printStackTrace();
}所以它首先要做的就是:它应该获取手机应该连接到的服务器ip,然后它将被插入到URL中,这样手机就可以连接到本地服务器并访问我的php文件。然而,当我在我的android手机上运行它的时候,它就崩溃了。为什么要这样做呢?希望你们能帮我解决这个问题。
发布于 2016-10-09 20:05:15
您不必动态请求服务器的IP地址。或者你有吗?您可以做的是获取服务器的静态IP地址(通过检查服务器的IP配置),并更改为
mWebview .loadUrl("http://your.ip.address.here/Lab4/Task1/index.php");
并移除您的
ip = InetAddress.getLocalHost();
ipv4add = ip.getHostAddress().toString();一旦您准备好部署,192.168.1.x将必须替换为您的活动域,例如
mWebview .loadUrl("http://www.yourdomain.com/Lab4/Task1/index.php");
此外,在您的清单中不要忘记这一点:
<uses-permission android:name="android.permission.INTERNET" />
https://stackoverflow.com/questions/39943200
复制相似问题