Skip to content Skip to sidebar Skip to footer

Android Webview - Access-control-allow-origin

XMLHttpRequest cannot load - Origin website... is not allowed by Access-Control-Allow-Origin.:1 I cannot load video inside a Webview. This is my log: 03-18 12:31:25.324: E/Web Co

Solution 1:

I've solved this with java.lang.reflect.Method:

1- Create a new class that extends webview, in this case 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")publicclassnewwebviewextendsWebView
{

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

    publicvoidenablecrossdomain()
    {
        try
        {
            Fieldfield= WebView.class.getDeclaredField("mWebViewCore");
            field.setAccessible(true);
            Object webviewcore=field.get(this);
            Methodmethod= 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+ publicvoidenablecrossdomain41()
    {
        try
        {
            Fieldwebviewclassic_field= WebView.class.getDeclaredField("mProvider");
            webviewclassic_field.setAccessible(true);
            Object webviewclassic=webviewclassic_field.get(this);
            Fieldwebviewcore_field= webviewclassic.getClass().getDeclaredField("mWebViewCore");
            webviewcore_field.setAccessible(true);
            Object mWebViewCore=webviewcore_field.get(webviewclassic);
            Fieldnativeclass_field= webviewclassic.getClass().getDeclaredField("mNativeClass");
            nativeclass_field.setAccessible(true);
            Object mNativeClass=nativeclass_field.get(webviewclassic);

            Methodmethod= mWebViewCore.getClass().getDeclaredMethod("nativeRegisterURLSchemeAsLocal",newClass[] {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 - Use the new class newwebview inside main activity , like this:

main.class

publicclassmainextendsActivity
{
    private newwebview webview;

    publicvoidonCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        webview = newnewwebview(this);
        WebSettingswebsettings= 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
    }
}

Source: http://blog.sina.com.cn/s/blog_723eed4f01018r9w.html (CHINESE)

Solution 2:

set this - Access-Control-Allow-Origin '*' header on your servers action that youre trying to call.

Post a Comment for "Android Webview - Access-control-allow-origin"