安卓webview无法加载本地服务器

5
我创建了一个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
请查看这个链接 - Hemant Parmar
我已经尝试过了,它不起作用! - Smit Sanghvi
1
你注意到日志中的这行了吗?/app_webview/variations_seed(没有此文件或目录) - Radesh
将您的手机和托管网页的机器连接到同一网络(wifi),然后在加载时使用webview.loadUrl("http://192.168.1.2:3000"),其中192.168.1.2是托管网页的机器IP地址。 - Shubham Jain
2个回答

5
如果您使用实体设备进行运行,则需要:
  1. 确保您的网站正在192.168.x.xxx:port上运行

  2. 在您的活动中设置完整的URL,例如:

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

  3. 在AndroidManifest.xml中添加

    android:usesCleartextTraffic =“true”

    以解决由于未使用https而导致的“不允许明文文本”错误

对于模拟器,请使用上一个答案中提到的10.0.2.2,并遵循第3步

1
使用此代码。
     webview.loadUrl("http://10.0.2.2:3000");

而不是

     webview.loadUrl("127.0.0.1:3000");

这也给我相同的输出。 - Smit Sanghvi
是的,仍然一样。 - Smit Sanghvi
你确定你的链接/端口工作正常吗?如果是,请查看你的通话日志并分享错误。 - Radesh
我猜React/Redux框架出了问题,因为我在从Android应用程序加载它时遇到了错误,但是从节点终端和浏览器中加载Web应用程序时没有任何错误。 - Smit Sanghvi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接