简体   繁体   English

Android WebView 未显示本地 IP 地址

[英]Android WebView not showing local IP address

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.我有一个运行WebIOPi的 Raspberry Pi,它连接到继电器板以通过基于 Web 的界面打开和关闭设备。 Here's what the interface looks like in a browser:这是浏览器中界面的外观:

在此处输入图像描述

Everything works fine, but I want to create an android app that would simply display the web-based interface via WebView.一切正常,但我想创建一个 android 应用程序,它可以简单地通过 WebView 显示基于 Web 的界面。 I've used WebView before and it seems pretty straight forward, but I can't get it working.我以前使用过 WebView,它看起来很简单,但我无法让它工作。

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:

<?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.我没有收到任何 logcat 错误,但是当我运行该应用程序时,我只得到一个白屏。 Also, if I substitute " http://www.google.com ", everything works fine.另外,如果我替换“ http://www.google.com ”,一切正常。 Also, I'm testing on a live device (no emulator).另外,我正在现场设备(无模拟器)上进行测试。 Any suggestions as to why this isn't working?关于为什么这不起作用的任何建议?

I have a problem like yours.我有和你一样的问题。 I've achieved load a local IP, but it takes at least 30 seconds, meanwhile white screen as you.我已经实现了加载本地IP,但至少需要30秒,同时白屏。 It loooks like chromium (webView) try to find the IP on internet, and at the end said: "oh, it's a local ip, let's load".看起来像chromium(webView)试图在互联网上找到IP,最后说:“哦,这是一个本地IP,让我们加载”。 It's quite weird.这很奇怪。 I keep on waiting for help as well.我也一直在等待帮助。

if you're using http protocol, try to add this on your android manifest如果您使用的是 http 协议,请尝试将其添加到您的 android 清单中

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 
       
}

May you need to bind your process to specify network?您可能需要绑定您的进程以指定网络吗?

Look at this solution: https://stackoverflow.com/a/46340497/2808872看看这个解决方案: https://stackoverflow.com/a/46340497/2808872

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM