简体   繁体   English

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

[英]Android webview not loading for localhost server

I have created a node.js web application and running it on port 3000. I wanted an android app for the same and created a webview based android app.我创建了一个 node.js web 应用程序并在端口 3000 上运行它。我想要一个相同的 android 应用程序并创建了一个基于 webview 的 android 应用程序。 While testing I was trying to give the localhost ip, the static server IP but the webview app is not loading in all of them.在测试时,我试图提供 localhost ip、静态服务器 IP,但 webview 应用程序并未在所有这些 IP 中加载。

Output:输出: 在此处输入图片说明

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)

I tried these IPs:我尝试了这些 IP:

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

All of them are working from my browser.他们都在我的浏览器中工作。

Mainactivity.java主要活动.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 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>

Use this用这个

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

Instead of代替

     webview.loadUrl("127.0.0.1:3000");

If you're using a physical device to run, then,如果你使用物理设备运行,那么,

  1. make sure your website is running at 192.168.x.xxx:port确保您的网站运行在 192.168.x.xxx:port

  2. set the full url in your activity, such as,在您的活动中设置完整网址,例如,

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

  3. add添加

    android:usesCleartextTraffic="true"机器人:使用CleartextTraffic =“真”

    in AndroidManifest.xml to fix 'cleartext not permitted' error caused for not using https在 AndroidManifest.xml 中修复因不使用 https 导致的“不允许明文”错误

For emulator use 10.0.2.2 as stated in the previous answer and follow the 3rd step对于模拟器使用 10.0.2.2 如上一个答案中所述并按照第 3 步操作

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

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