混合app打成aar包后 ajax访问无效问题

将项目打包成aar后,继承到其他项目中。Ajax访问无效,可能是跨域的问题。

解决方案: 在loadurl的地方加上以下代码。

try {
   if (Build.VERSION.SDK_INT >= 16) {
      Class<?> clazz = webView.getSettings().getClass();
      Method method = clazz.getMethod(
            "setAllowUniversalAccessFromFileURLs", boolean.class);
      if (method != null) {
         method.invoke(webView.getSettings(), true);
      }
   }
} catch (IllegalArgumentException e) {
   e.printStackTrace();
} catch (NoSuchMethodException e) {
   e.printStackTrace();
} catch (IllegalAccessException e) {
   e.printStackTrace();
} catch (InvocationTargetException e) {
   e.printStackTrace();
}
 

try {
   if (Build.VERSION.SDK_INT >= 16) {
      Class<?> clazz = webView.getSettings().getClass();
      Method method = clazz.getMethod(
            "setAllowUniversalAccessFromFileURLs", boolean.class);
      if (method != null) {
         method.invoke(webView.getSettings(), true);
      }
   }
} catch (IllegalArgumentException e) {
   e.printStackTrace();
} catch (NoSuchMethodException e) {
   e.printStackTrace();
} catch (IllegalAccessException e) {
   e.printStackTrace();
} catch (InvocationTargetException e) {
   e.printStackTrace();
}

猜你喜欢

转载自blog.csdn.net/weixin_42171638/article/details/84315608