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 ?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With