Android WebView not showing local IP address

Viewed 2943

I have a Raspberry Pi running WebIOPi, which is connected to a relay board to turn things on and off via a web-based interface. Here's what the interface looks like in a browser:

enter image description here

Everything works fine, but I want to create an android app that would simply display the web-based interface via WebView. I've used WebView before and it seems pretty straight forward, but I can't get it working.

Here's my code:

MainActivity:

package org.kevinbright.android.backyardcontrolapp;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MainActivity extends Activity {

    private WebView mWebViewer;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    //url = getIntent().getStringExtra(EXTRA_URL);

    mWebViewer = (WebView)findViewById(R.id.webviewer);
    mWebViewer.setWebViewClient(new WebViewClient());
    mWebViewer.clearCache(true);
    mWebViewer.getSettings().setJavaScriptEnabled(true);
    mWebViewer.loadUrl("http://192.168.1.100:8000/");
}
}

Here's the XML:

<?xml version="1.0" encoding="UTF-8"?>

<LinearLayout android:orientation="vertical"
              android:layout_height="match_parent"
              android:layout_width="match_parent"
              xmlns:android="http://schemas.android.com/apk/res/android">

<WebView android:layout_height="match_parent"
         android:layout_width="match_parent"
         android:id="@+id/webviewer"/>

And I added:

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

to the Manifest.

I don't get any logcat errors, but when I run the app, I get only a white screen. Also, if I substitute "http://www.google.com", everything works fine. Also, I'm testing on a live device (no emulator). Any suggestions as to why this isn't working?

3 Answers

if you're using http protocol, try to add this on your android manifest

android:usesCleartextTraffic="true"

it works for me

public void onCreate(Bundle savedInstanceState) {
   
        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");
        //pass complete url 
       
}
Related