gpt4 book ai didi

android webview - 访问控制允许来源

转载 作者:太空狗 更新时间:2023-10-29 15:26:50 25 4
gpt4 key购买 nike

XMLHttpRequest cannot load  - Origin website... is not allowed by Access-Control-Allow-Origin.:1

我无法在 Webview 中加载视频。

这是我的日志:

03-18 12:31:25.324: E/Web Console(7074): XMLHttpRequest cannot load http://3.stream.site.com/cam/en/watch/test?gravityCookieId=ba6605bdb69d29163fb4d97594fc8b169&cams_session=6a2294654ccadg6854c21edc3e6598cfa&isPromo=&isHls=. Origin http://m.site1.com is not allowed by Access-Control-Allow-Origin.:1
03-18 12:31:25.324: E/Web Console(7074): Ajax Handler Error: url: http://3.stream.site2.com/cam/en/watch/test?gravityCookieId=ba6605bdb69d29163fb4d97594fc8b169&cams_session=6a2294654ccadg6854c21edc3e6598cfa&isPromo=&isHls= || status:0|| statusText: error|| responseText: :4620

甚至使用:

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
websettings.setAllowUniversalAccessFromFileURLs(true);
websettings.setAllowFileAccessFromFileURLs(true);
websettings.setAllowContentAccess(true);
websettings.setAppCacheEnabled(true);
}

解决这个问题的方法是什么?

最佳答案

我用 java.lang.reflect.Method 解决了这个问题:

1- 创建一个扩展 webview 的新类,在本例中为 newwebview:

newwebview.class

package my.pkg.name;

import java.lang.reflect.Method;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.webkit.WebView;

@SuppressLint("Instantiatable")
public class newwebview extends WebView
{

@SuppressLint("Instantiatable")
public newwebview(Context context)
{
super(context);
// TODO Auto-generated constructor stub
}

public void enablecrossdomain()
{
try
{
Field field = WebView.class.getDeclaredField("mWebViewCore");
field.setAccessible(true);
Object webviewcore=field.get(this);
Method method = webviewcore.getClass().getDeclaredMethod("nativeRegisterURLSchemeAsLocal", String.class);
method.setAccessible(true);
method.invoke(webviewcore, "http");
method.invoke(webviewcore, "https");
}
catch(Exception e)
{
Log.d("wokao","enablecrossdomain error");
e.printStackTrace();
}
}

//for android 4.1+
public void enablecrossdomain41()
{
try
{
Field webviewclassic_field = WebView.class.getDeclaredField("mProvider");
webviewclassic_field.setAccessible(true);
Object webviewclassic=webviewclassic_field.get(this);
Field webviewcore_field = webviewclassic.getClass().getDeclaredField("mWebViewCore");
webviewcore_field.setAccessible(true);
Object mWebViewCore=webviewcore_field.get(webviewclassic);
Field nativeclass_field = webviewclassic.getClass().getDeclaredField("mNativeClass");
nativeclass_field.setAccessible(true);
Object mNativeClass=nativeclass_field.get(webviewclassic);

Method method = mWebViewCore.getClass().getDeclaredMethod("nativeRegisterURLSchemeAsLocal",new Class[] {int.class,String.class});
method.setAccessible(true);
method.invoke(mWebViewCore,mNativeClass, "http");
method.invoke(mWebViewCore,mNativeClass, "https");
}
catch(Exception e)
{
Log.d("wokao","enablecrossdomain error");
e.printStackTrace();
}
}

2 - 在 main Activity 中使用新类 newwebview ,如下所示:

主类

public class main extends Activity
{
private newwebview webview;

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

webview = new newwebview(this);
WebSettings websettings = webview.getSettings();

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
{
webview.enablecrossdomain41();

websettings.setAllowUniversalAccessFromFileURLs(true);
websettings.setAllowFileAccessFromFileURLs(true);

}
else
{
webview.enablecrossdomain();
}


//rest of the code here
}
}

来源:http://blog.sina.com.cn/s/blog_723eed4f01018r9w.html (中文)

关于android webview - 访问控制允许来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22479677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com