【问题标题】:Keep getting "A network error occurred" when testing PhoneGap app with Eclipse使用 Eclipse 测试 PhoneGap 应用程序时不断收到“发生网络错误”
【发布时间】:2025-11-21 07:30:02
【问题描述】:

我刚刚从分包商那里收到了 PhoneGap 解决方案,以便在发布前在我的手机上进行测试。

我将项目导入 Eclipse,一切看起来都很好。

我可以通过打开 index.html 文件在我的计算机上本地测试应用程序:

file://E:/AppDevelopment/Workspace/myproject/assets/www/index.html

到目前为止一切顺利。然后我尝试在我的手机上启动它(通过 USB 电缆)。首页打开,但缺少 CSS。当我点击任何链接时,我会收到以下消息:

"A network error occurred. (file:///android_asset/www/car.html?carID=106"

有没有人遇到过类似的问题?关于如何调试错误的任何建议?

更新

按照 Dmyto 的建议,我正在尝试更改 onReceivedError。但我只会遇到编码错误。

package com.phonegap.mysite;

import android.os.Bundle;
import org.apache.cordova.*;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Mysiteextends DroidGap {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");

    }

/** This is where it goes wrong **/

    WebView webview = new WebView(this);

    webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Log.i("WEB_VIEW_TEST", "error code:" + errorCode);
                super.onReceivedError(view, errorCode, description, failingUrl);
        }

}

【问题讨论】:

    标签: android eclipse cordova


    【解决方案1】:

    如果没有看到您的 index.html 代码,很难判断您是否正确引用了您的 css 文件。此外,该网址:file:///android_asset/www/car.html=carID=106 似乎无效? =carID=106 是怎么回事?正确的 URL 应该是 ?carID=106。但等等,Android 上存在一个错误,即无法正确读取 url 参数。

    这是 Android 上的一个错误。你应该去给它加星:

    https://code.google.com/p/android/issues/detail?id=17535
    

    同时使用localStorage.setItem()/getItem()在页面之间传递值。

    【讨论】:

    • 抱歉,网址打错了。我使用的是? 而不是=。正确的网址是file:///android_asset/www/car.html?carID=106。正如我在 Q 中所说,当我在我的计算机(和 Eclipse)上测试它时,该应用程序运行良好,而不是在我的手机上。我没有为 Android 编写应用程序的经验,所以我对各种代码的何处一无所知。
    • 是的,所以你被 Android 操作系统中的错误绊倒了。因此,如果您想将信息从一个页面传递到另一个页面,则需要使用 localStorage。
    • 经过一些测试,你是绝对正确的。我的问题是;使用 localStorage 是否适用于 iOS 和 Windows 手机?
    • 我确信它适用于 iOS,我很确定它适用于 WP,但我还没有测试过其中之一。
    【解决方案2】:

    您可以将WebViewClient 设置为您的网络视图,并覆盖onReceivedError 方法。

    【讨论】:

    【解决方案3】:

    您可能需要使用 IceCreamCordovaWebViewClient

    @Override
        public void init() {
        super.init(webView, new IceCreamCordovaWebViewClient(this, webView), new CordovaChromeClient(this, webView));
    }
    

    【讨论】: