Cordova 5.1.1“出现网络错误";当我从Cordova Android Webview调用网络URL时,onReceivedError方法中的错误消息 [英] Cordova 5.1.1 "There was a network error" message in onReceivedError method when I call network url from Cordova Android webview

查看:132
本文介绍了Cordova 5.1.1“出现网络错误";当我从Cordova Android Webview调用网络URL时,onReceivedError方法中的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Cordova 5.1.1.我想从android的CordovaWebview调用网络URL.我的Android操作系统版本为4.4.2.这是我来自Android Side的代码.

I am using Cordova 5.1.1. I want to call network URL from CordovaWebview in android. My Android OS version is 4.4.2. Here is my code from Android Side.

Android

Android

content_main.xml文件:-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    <org.apache.cordova.engine.SystemWebView
        android:id="@+id/cordovaWebView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

这是我的Java文件,在该文件中我已初始化WebView并调用了网络URL.

Here is my java file where i have initialized WebView and call a network url.

MainActivity.java:-

package com.andapp.kushpatel.cordova5_file_chooser;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import org.apache.cordova.ConfigXmlParser;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaInterfaceImpl;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaWebViewEngine;
import org.apache.cordova.CordovaWebViewImpl;
import org.apache.cordova.engine.SystemWebView;
import org.apache.cordova.engine.SystemWebViewEngine;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity implements CordovaInterface{


    private static String TAG = MainActivity.class.getSimpleName();
    private CordovaWebView cordovaWebView;
    public static final String START_URL = "http://localhost:8080/GCM_Project/";

    CordovaPlugin activityResultCallback;
    private Object activityResultKeepRunning;
    private Object keepRunning;
    private final ExecutorService threadPool = Executors.newCachedThreadPool();

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


        //Set up the webview
            ConfigXmlParser parser = new ConfigXmlParser();
            parser.parse(this);

        SystemWebView syswebView = (SystemWebView) findViewById(R.id.cordovaWebView);
        cordovaWebView = new CordovaWebViewImpl(new SystemWebViewEngine(syswebView));
        cordovaWebView.init(this, parser.getPluginEntries(), parser.getPreferences());
        cordovaWebView.loadUrl(START_URL);



    }

    @Override
    public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {
        this.activityResultCallback = command;
        this.activityResultKeepRunning = this.keepRunning;

        // If multitasking turned on, then disable it for activities that return results
        if (command != null) {
            this.keepRunning = false;
        }

        // Start activity
        super.startActivityForResult(intent, requestCode);
    }

    @Override
    public void setActivityResultCallback(CordovaPlugin plugin) {
        this.activityResultCallback = plugin;
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public Object onMessage(String s, Object o) {
        return null;
    }

    @Override
    public ExecutorService getThreadPool() {
        return threadPool;
    }

    @Override
    public void requestPermission(CordovaPlugin cordovaPlugin, int i, String s) {

    }

    @Override
    public void requestPermissions(CordovaPlugin cordovaPlugin, int i, String[] strings) {

    }

    @Override
    public boolean hasPermission(String s) {
        return false;
    }
}

这是我的配置文件,其中我使用了两个Cordova插件.

Here is my config file where I have used two Cordova plugins.

config.xml

<widget id="io.cordova.helloCordova" version="2.0.0" xmlns="http://www.w3.org/ns/widgets">
    <name>Hello Cordova</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <access origin="http://*/*"/>
    <access origin="https://*/*"/>
    <access origin="*.apache.org" />
    <access origin="http://*.google.com/*" />
    <access origin="https://*.google.com/*" />
    <access origin="https://*.googleapis.com/*" />
    <access origin="https://*.gstatic.com/*" />
    <access origin="tel:*" launch-external="yes"/>
    <access origin="geo:*" launch-external="yes"/>
    <access origin="mailto:*" launch-external="yes"/>
    <access origin="sms:*" launch-external="yes"/>
    <access origin="market:*" launch-external="yes"/>
    <content src="index.html" />

    <allow-navigation href="http://*/*" />
    <allow-navigation href="https://*/*" />

    <preference name="loglevel" value="DEBUG" />

    <feature name="FileChooser">
        <param name="android-package" value="com.andapp.kushpatel.cordova5_file_chooser.FileChooser" />
    </feature>

    <feature name="WhitelistPlugin">
        <param name="android-package" value="com.andapp.kushpatel.cordova5_file_chooser.WhitelistPlugin" />
    </feature>

    <preference name="loadUrlTimeoutValue" value="300000" />
</widget>

在服务器端有一个html文件.在该html文件中,我已将meta标签用于whiteList插件.

At server side there is a html file. In that html file I have used meta tag for whiteList plugin.

 <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'"/>

现在,当我打电话说在Web视图和日志中什么也没显示时,我会收到发生网络错误" 错误.

Now when I call that never nothing displays in webview and in log i am getting "There was a network error" error.

我已在以下链接中找到了所描述的代码. https://cordova.apache.org/docs/en/6.x/reference/cordova-plugin-whitelist/

I have refered below links for described code. https://cordova.apache.org/docs/en/6.x/reference/cordova-plugin-whitelist/

如何使用白色配置Cordova-android 4.0 -列表

>无内容-找到安全性策略元标记.我的phonegap应用程序出现错误

推荐答案

您必须添加以下行才能添加插件.现在它将正常工作.

You have to add this lines to add your plugin. And now it will work properly.

ConfigXmlParser parser = new ConfigXmlParser();
        parser.parse(mActivity); 

CordovaPlugin plugin = new WhitelistPlugin(mActivity);
                PluginEntry entry = new PluginEntry("whiteList",plugin);
                parser.getPluginEntries().add(entry);

这篇关于Cordova 5.1.1“出现网络错误";当我从Cordova Android Webview调用网络URL时,onReceivedError方法中的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆