android webview - Access-Control-Allow-Origin

时间:2014-03-18 12:38:12

标签: android webview

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

我无法在网络视图中加载视频。

这是我的日志:

03-18 12:31:25.324: E/Web Console(7074): XMLHttpRequest cannot load http://3.stream.site.com/mycams/en/watch/test?gravityCookieId=ba6605bdb69d29163fb4d97594fc8b169&mycams_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/mycams/en/watch/test?gravityCookieId=ba6605bdb69d29163fb4d97594fc8b169&mycams_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);
        } 

解决方案是什么?

2 个答案:

答案 0 :(得分:2)

我用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 - 在newwebview活动中使用新课程main,如下所示:

<强> main.class

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(中文)

答案 1 :(得分:-3)

设置此项 - Access-Control-Allow-Origin&#39; *&#39;您尝试呼叫的服务器上的标题。