问题描述
我一直在开发*与科尔多瓦(又名PhoneGap的)为Android远远超过一年了,我试图让我的应用程序可在果冻豆运行,但我收到以下错误:
I've been developing* with Cordova (aka Phonegap) for Android for well over a year now and am trying to make my apps available to run in Jelly Bean, but I am getting the following error:
XMLHttpRequest cannot load http://127.0.0.1:40582/[somerandomstring]. Origin null is not allowed by Access-Control-Allow-Origin. at null:1
(以及类似的错误任何后续AJAX请求使用本地主机或文件://)只是为了测试,我承认获得的一切在config.xml中的部分访问控制 - 允许 - 原产地
(and similar errors for any subsequent ajax requesting use of localhost or file://)Just to test, I grant access to everything in the config.xml in the section for Access-Control-Allow-Origin
<access origin="*"/>
<access origin="http://127.0.0.1*"/>
在我的研究我已经发现的错误与,谷歌作出的Android果冻豆的设置改变。以下是我发现:从:<一href="https://git-wip-us.apache.org/repos/asf?p=incubator-cordova-android.git;a=commitdiff;h=07439ff9" rel="nofollow">https://git-wip-us.apache.org/repos/asf?p=incubator-cordova-android.git;a=commitdiff;h=07439ff9
In my research I have discovered the error is related to a setting change that Google made as of Android Jelly Bean. Here is what I found:From: https://git-wip-us.apache.org/repos/asf?p=incubator-cordova-android.git;a=commitdiff;h=07439ff9
- 这是从org.apache.cordova.CordovaWebView
-- This is from org.apache.cordova.CordovaWebView
// Jellybean rightfully tried to lock this down. Too bad they didn't give us a whitelist
// while we do this
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
Level16Apis.enableUniversalAccess(settings);
- 这也是从org.apache.cordova.CordovaWebView
-- This is also from org.apache.cordova.CordovaWebView
// Wrapping these functions in their own class prevents warnings in adb like:
// VFY: unable to resolve virtual method 285: Landroid/webkit/WebSettings;.setAllowUniversalAccessFromFileURLs
@TargetApi(16)
private static class Level16Apis {
static void enableUniversalAccess(WebSettings settings) {
settings.setAllowUniversalAccessFromFileURLs(true);
}
}
这是很好的科尔多瓦试图解决的改变,但不幸的是,这并不工作...
It's nice that Cordova tried to work around the change, but unfortunately this does not work...
在这些所谓的线程 href="http://stackoverflow.com/a/11511062/1013526">和的我发现了一个通用的解决方案,简单地更改设置如下:
In these SO threads here and here I found a common solution, to simply change a setting as follows:
if(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
super.appView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
现在,我得到以下警告:
Now I get the following warning:
Call requires API level 16 (current min is 8)
android.webkit.WebSettings#setAllowUniversalAccessFromFileURLs
下面是我在我的AndroidManifest.xml中的API
Here is what I have for the api in my AndroidManifest.xml
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" />
为什么需要我去的minSdkVersion更改为16,而不是按照我的targetSdkVersion这是16?
Why is it requiring me to change the minSdkVersion to 16 rather than follow my targetSdkVersion which is 16?
思考?
备注:我目前使用的科尔多瓦2.0,Eclipse的靛蓝SR2(所有更新电流),Android的SDK(所有更新电流),在Windows 7家庭(所有更新电流),Java 7的更新7。
Notes: I'm currently using Cordova 2.0, Eclipse Indigo SR2 (all updates current), Android SDK (all updates current), on Windows 7 Home (all updates current), Java 7 Update 7.
推荐答案
行,所以一吨搜索,猜测和检查后,我找到了一个可行的解决方案。
OK so after a ton of searching, guessing, and checking, I found a workable solution.
我要创建一个单独的功能,为setAllowUniversalAccessFromFileURLs打电话......那固定的TargetApi问题,但随后presented一个不同的软糖它不会连接到我在我的使用loadURL调用,所以我的文件不得不重写onReceivedError功能。这是我得到的code:
I had to create a separate function for the setAllowUniversalAccessFromFileURLs call... That fixed the TargetApi issue but then presented a different one on JellyBean where it wouldn't connect to the file I had in my loadURL call so I had to override the onReceivedError function. Here is my resulting code:
package com.MyUniqueDomain.MyUniquePackage;
import android.annotation.TargetApi;
import android.os.Bundle;
import org.apache.cordova.*;
public class MainActivity extends DroidGap {
private int retryCount = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setStringProperty("loadingDialog", "Please wait -- loading...");
super.init();
if(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
fixJellyBeanIssues();
}
super.loadUrl("file:///android_asset/www/index.html");
}
@TargetApi(16)
protected void fixJellyBeanIssues() {
System.out.println(super.appView.toString());
try {
super.appView.getSettings().setAllowUniversalAccessFromFileURLs(true);
} catch(NullPointerException e) {
System.out.println(e.toString());
}
}
// catch an error and if try again 1x or quit
@Override
public void onReceivedError( int errorCode, String description, String failingUrl)
{
if(retryCount < 3) {
retryCount++;
System.out.println("Connection failed, trying again. Retry Count: "+retryCount);
super.loadUrl("file:///android_asset/www/index.html");
} else {
System.out.println("Sorry, it failed three times so I give up.");
super.loadUrl("file:///android_asset/www/fail.html");
}
return;
}
}
这篇关于PhoneGap的/科尔多瓦应用打破了果冻豆 - 访问控制 - 允许 - 起源和setAllowUniversalAccessFromFileURLs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!