无法为本地服务器加载Android Webview

时间:2018-09-25 07:38:43

标签: java android node.js reactjs nodes

我创建了一个node.js Web应用程序并在端口3000上运行。我想要一个同样的Android应用程序,并创建一个基于Webview的Android应用程序。 在测试时,我尝试为本地IP提供静态服务器IP,但webview应用并未全部加载。

输出: enter image description here

Android logcat:

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>

2 个答案:

答案 0 :(得分:1)

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

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

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

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

  3. 添加

    android:usesCleartextTraffic="true"

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

对于模拟器使用 10.0.2.2 如上一个答案中所述并按照第 3 步操作

答案 1 :(得分:0)

使用此

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

代替

     webview.loadUrl("127.0.0.1:3000");