Francis Snipe
Francis Snipe

Reputation: 551

Android webview can't connect to local ip address

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.

Upvotes: 4

Views: 11095

Answers (2)

user5642169
user5642169

Reputation: 39

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!

Upvotes: 3

Stefan
Stefan

Reputation: 1084

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

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

Upvotes: 4

Related Questions