Android webview can't connect to local ip address

好久不见. 提交于 2019-12-24 00:44:45

问题


I've got a webView class in a simple android app. I'm trying to get webView to load a webpage hosted on a server on my local network and it can't connect.

   WebView webview = new WebView(this);
    setContentView(webview);
    webview.loadUrl("192.168.1.104");

webView says 'web page not available'

I can connect to 192.168.1.104 in a regular browser and any mobile browser. When I replace 192.168.1.104 with http://example.com it works.


回答1:


Did you add the internet permission to the AndroidManifest.xml?

<uses-permission android:name="android.permission.INTERNET"/> 



回答2:


just add those code below it will show your page in lan:

public void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "Entering onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);

    mWebView = (WebView)findViewById(R.id.webview);
    mWebView.clearCache(true);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.loadUrl("http://10.0.2.2:8080/SampleWebServer/Welcome.html");
    Log.i(TAG,"Exiting onCreate");

}

mWebView.getSettings().setJavaScriptEnabled(true);

that's it!



来源:https://stackoverflow.com/questions/15933075/android-webview-cant-connect-to-local-ip-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!