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/cam/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/cam/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);
        }

解决办法是什么?

最佳答案

我已经用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,如下所示:
主级
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(中文)

10-08 17:53