3
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 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

Even using:

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

What's the solution for this ?

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268

2 Answers2

2

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")
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 - Use the new class newwebview inside main activity , like this:

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
    }
}

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

Community
  • 1
  • 1
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • I had the same problem, and I have applied your patch, and it partially works, it seems to repair some sites while break others. for example if I load m.facebook.com with the patch, the page keeps redirect me to an error page... any ideas? – TacB0sS Apr 26 '14 at 19:20
  • Try setting a different user agent. – Pedro Lobito Apr 26 '14 at 22:06
  • I used it in a project 4 years ago, it did the job by then, but I'm not sure now. Give it a try. – Pedro Lobito Oct 15 '18 at 12:15
  • 1
    It no longer works, google removed the mWebViewCore and I can't find a nativeRegisterURLSchemeAsLocal method anymore on current webkit code. – jcesarmobile Aug 26 '19 at 16:31
-3

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