java - Android webview 未加载本地主机服务器

标签 java android node.js reactjs nodes

我创建了一个 Node.js Web 应用程序并在端口 3000 上运行它。我想要一个相同的 Android 应用程序,并创建了一个基于 WebView 的 Android 应用程序。 在测试时,我试图提供本地主机 IP、静态服务器 IP,但 WebView 应用程序并未在所有这些中加载。

输出: enter image description here

Android 日志猫:

09-25 20:54:12.508 7589-7589/? I/.cloudassigmen: Not late-enabling -Xcheck:jni (already on)
09-25 20:54:13.288 7589-7589/? W/.cloudassigmen: Unexpected CPU variant for X86 using defaults: x86
09-25 20:54:15.410 7589-7589/com.example.smitsanghvi.cloudassigment W/.cloudassigmen: JIT profile information will not be recorded: profile file does not exits.
09-25 20:54:15.431 7589-7589/com.example.smitsanghvi.cloudassigment I/chatty: uid=10085(com.example.smitsanghvi.cloudassigment) identical 10 lines
09-25 20:54:15.432 7589-7589/com.example.smitsanghvi.cloudassigment W/.cloudassigmen: JIT profile information will not be recorded: profile file does not exits.
09-25 20:54:15.740 7589-7589/com.example.smitsanghvi.cloudassigment I/InstantRun: starting instant run server: is main process
09-25 20:54:17.026 7589-7589/com.example.smitsanghvi.cloudassigment W/.cloudassigmen: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
09-25 20:54:17.033 7589-7589/com.example.smitsanghvi.cloudassigment W/.cloudassigmen: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
09-25 20:54:17.108 7589-7589/com.example.smitsanghvi.cloudassigment I/WebViewFactory: Loading com.android.chrome version 69.0.3497.100 (code 349710012)
09-25 20:54:17.599 7589-7589/com.example.smitsanghvi.cloudassigment I/cr_LibraryLoader: Time to load native libraries: 25 ms (timestamps 6319-6344)
09-25 20:54:17.744 7589-7589/com.example.smitsanghvi.cloudassigment I/chromium: [INFO:library_loader_hooks.cc(36)] Chromium logging enabled: level = 0, default verbosity = 0
09-25 20:54:17.745 7589-7589/com.example.smitsanghvi.cloudassigment I/cr_LibraryLoader: Expected native library version number "69.0.3497.100", actual native library version number "69.0.3497.100"
09-25 20:54:17.758 7589-7615/com.example.smitsanghvi.cloudassigment E/cr_VariationsUtils: Failed reading seed file "/data/user/0/com.example.smitsanghvi.cloudassigment/app_webview/variations_seed": /data/user/0/com.example.smitsanghvi.cloudassigment/app_webview/variations_seed (No such file or directory)
09-25 20:54:17.788 7589-7616/com.example.smitsanghvi.cloudassigment W/cr_ChildProcLH: Create a new ChildConnectionAllocator with package name = com.android.chrome, sandboxed = true
09-25 20:54:17.856 7589-7589/com.example.smitsanghvi.cloudassigment I/cr_BrowserStartup: Initializing chromium process, singleProcess=false
09-25 20:54:18.541 7589-7589/com.example.smitsanghvi.cloudassigment W/.cloudassigmen: Accessing hidden method Landroid/view/textclassifier/logging/SmartSelectionEventTracker;-><init>(Landroid/content/Context;I)V (light greylist, reflection)
    Accessing hidden method Landroid/view/textclassifier/logging/SmartSelectionEventTracker;->logEvent(Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent;)V (light greylist, reflection)
09-25 20:54:18.542 7589-7589/com.example.smitsanghvi.cloudassigment W/.cloudassigmen: Accessing hidden method Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent;->selectionStarted(I)Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent; (light greylist, reflection)
    Accessing hidden method Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent;->selectionModified(II)Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent; (light greylist, reflection)
    Accessing hidden method Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent;->selectionModified(IILandroid/view/textclassifier/TextClassification;)Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent; (light greylist, reflection)
    Accessing hidden method Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent;->selectionModified(IILandroid/view/textclassifier/TextSelection;)Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent; (light greylist, reflection)
    Accessing hidden method Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent;->selectionAction(III)Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent; (light greylist, reflection)
    Accessing hidden method Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent;->selectionAction(IIILandroid/view/textclassifier/TextClassification;)Landroid/view/textclassifier/logging/SmartSelectionEventTracker$SelectionEvent; (light greylist, reflection)
09-25 20:54:18.720 7589-7638/com.example.smitsanghvi.cloudassigment W/cr_media: Requires BLUETOOTH permission
09-25 20:54:18.761 7589-7625/com.example.smitsanghvi.cloudassigment D/NetworkSecurityConfig: No Network Security Config specified, using platform default
09-25 20:54:18.806 7589-7589/com.example.smitsanghvi.cloudassigment D/OpenGLRenderer: Skia GL Pipeline
09-25 20:54:19.025 7589-7662/com.example.smitsanghvi.cloudassigment I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
09-25 20:54:19.027 7589-7662/com.example.smitsanghvi.cloudassigment I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
09-25 20:54:19.038 7589-7662/com.example.smitsanghvi.cloudassigment D/EGL_emulation: eglCreateContext: 0xcf803840: maj 3 min 0 rcv 3
09-25 20:54:19.039 7589-7662/com.example.smitsanghvi.cloudassigment D/EGL_emulation: eglMakeCurrent: 0xcf803840: ver 3 0 (tinfo 0xcf708f90)
09-25 20:54:19.262 7589-7667/com.example.smitsanghvi.cloudassigment I/OpenGLRenderer: Initialized EGL, version 1.4
09-25 20:54:19.262 7589-7667/com.example.smitsanghvi.cloudassigment D/OpenGLRenderer: Swap behavior 1
09-25 20:54:19.265 7589-7667/com.example.smitsanghvi.cloudassigment W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
09-25 20:54:19.266 7589-7667/com.example.smitsanghvi.cloudassigment D/OpenGLRenderer: Swap behavior 0
09-25 20:54:19.273 7589-7667/com.example.smitsanghvi.cloudassigment D/EGL_emulation: eglCreateContext: 0xcf8035a0: maj 3 min 0 rcv 3
09-25 20:54:19.292 7589-7667/com.example.smitsanghvi.cloudassigment D/EGL_emulation: eglMakeCurrent: 0xcf8035a0: ver 3 0 (tinfo 0xcf74e2b0)
09-25 20:54:19.638 7589-7667/com.example.smitsanghvi.cloudassigment D/EGL_emulation: eglMakeCurrent: 0xcf8035a0: ver 3 0 (tinfo 0xcf74e2b0)
09-25 20:54:19.896 7589-7662/com.example.smitsanghvi.cloudassigment W/VideoCapabilities: Unrecognized profile 4 for video/hevc
09-25 20:54:20.048 7589-7662/com.example.smitsanghvi.cloudassigment I/VideoCapabilities: Unsupported profile 4 for video/mp4v-es
09-25 20:54:20.107 7589-7667/com.example.smitsanghvi.cloudassigment I/OpenGLRenderer: Davey! duration=900ms; Flags=0, IntendedVsync=2307946960898, Vsync=2308346960882, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=2308351487351, AnimationStart=2308351545351, PerformTraversalsStart=2308351596351, DrawStart=2308377901351, SyncQueued=2308383974351, SyncStart=2308389276351, IssueDrawCommandsStart=2308391431351, SwapBuffers=2308827224351, FrameCompleted=2308853055351, DequeueBufferDuration=328000, QueueBufferDuration=1955000, 
09-25 20:54:20.107 7589-7662/com.example.smitsanghvi.cloudassigment W/cr_MediaCodecUtil: HW encoder for video/avc is not available on this device.
09-25 20:54:20.291 7589-7613/com.example.smitsanghvi.cloudassigment W/cr_CrashFileManager: /data/user/0/com.example.smitsanghvi.cloudassigment/cache/WebView/Crash Reports does not exist or is not a directory
09-25 20:54:23.405 7589-7662/com.example.smitsanghvi.cloudassigment D/EGL_emulation: eglCreateContext: 0xca251560: maj 3 min 0 rcv 3
09-25 20:54:23.406 7589-7662/com.example.smitsanghvi.cloudassigment D/EGL_emulation: eglMakeCurrent: 0xca251560: ver 3 0 (tinfo 0xcf708f90)
09-25 20:54:31.146 7589-7589/com.example.smitsanghvi.cloudassigment I/chromium: [INFO:CONSOLE(32048)] source: http://10.0.2.2:3000/static/js/bundle.js (32048)
09-25 20:54:31.679 7589-7589/com.example.smitsanghvi.cloudassigment I/chromium: [INFO:CONSOLE(43724)] "Uncaught TypeError: Cannot read property 'apply' of undefined", source: http://10.0.2.2:3000/static/js/bundle.js (43724)

我尝试了这些 IP:

localhost:3000
http://127.0.0.1:3000/
http://10.0.2.2:3000

所有这些都可以在我的浏览器中运行。

Mainactivity.java

package com.example.smitsanghvi.cloudassigment;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
    private WebView webview;

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

        webview = (WebView) findViewById(R.id.webview);
        webview.setWebViewClient(new WebViewClient());
        webview.loadUrl("127.0.0.1:3000");
        WebSettings webSettings=webview.getSettings();
        webSettings.setJavaScriptEnabled(true);

    }

    @Override
    public void onBackPressed() {
        if(webview.canGoBack())
        {
            webview.goBack();
        }
        else {
            super.onBackPressed();
        }


    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.smitsanghvi.cloudassigment">
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

最佳答案

如果您使用物理设备运行,那么,

  1. 确保您的网站在 192.168.x.xxx:port 上运行

  2. 在您的 Activity 中设置完整的网址,例如,

    webview.loadUrl("http://192.168.1.107:3000/");

  3. 添加

    android:usesCleartextTraffic="true"

    在 AndroidManifest.xml 中修复因不使用 https 导致的“不允许明文”错误

对于模拟器,请使用上一个答案中所述的 10.0.2.2 并按照第三步操作

关于java - Android webview 未加载本地主机服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52492970/

相关文章:

java - 如何将 json 从 primefaces 发送到 jquery

java - 使用java源码调试Android库项目

android - 将可隐藏的标题 View 添加到 recyclerview

node.js - WebSocket 连接在端口 8080 或 80 上的移动网络中失败

node.js - 调试时 "Run-Script"如何在 VS Code 中为 node.js 应用程序工作?

java - 从 Web 访问文件系统的 Java Applet 替代方案

java - 如何更改 jcombobox 中 jpopupmenu 的高度?

java - 如何启用 Collection notifyDataChange?

android - 使用 ORMLITE 在 Android 中的多个查询条件

node.js - Node.js 终端中漂亮的异常消息