【问题标题】:Android Lollipop WebView behind Proxy won't work代理后面的 Android Lollipop WebView 不起作用
【发布时间】:2015-10-05 15:27:26
【问题描述】:

我正在尝试通过我在 Lollipop 中的 Android 应用访问互联网。该应用程序有一个 WebView。出于某种原因,当我在公司代理后面时,WebView 不允许我访问 Internet(尽管我可以通过 Android Chrome 浏览器访问所需的页面)。在搜索了两天的解决方案后,我决定在这里寻求解决方案。以下是根据其他一些帖子应该解决问题的方法,但事实并非如此。它抛出一个异常。

 private void setProxy(String host, int port) {
    Log.v("WebViewProxy", String.format("Setting proxy (%s, %s) with >= 5.0 API.", host, port));

    System.setProperty("http.proxyHost", host);
    System.setProperty("http.proxyPort", port + "");
    System.setProperty("https.proxyHost", host);
    System.setProperty("https.proxyPort", port + "");

    try {
        Context appContext = getContext().getApplicationContext();
        Class<?> applictionClass = Class.forName("android.app.Application");
        Field mLoadedApkField = applictionClass.getDeclaredField("mLoadedApk");
        mLoadedApkField.setAccessible(true);
        Object mloadedApk = mLoadedApkField.get(appContext);
        Class<?> loadedApkClass = Class.forName("android.app.LoadedApk");
        Field mReceiversField = loadedApkClass.getDeclaredField("mReceivers");
        mReceiversField.setAccessible(true);
        ArrayMap<?, ?> receivers = (ArrayMap<?, ?>) mReceiversField.get(mloadedApk);
        for (Object receiverMap : receivers.values()) {
            for (Object receiver : ((ArrayMap<?, ?>) receiverMap).keySet()) {
                Class<?> clazz = receiver.getClass();
                if (clazz.getName().contains("ProxyChangeListener")) {
                    Method onReceiveMethod = clazz.getDeclaredMethod("onReceive",
                            Context.class, Intent.class);
                    Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
                    onReceiveMethod.invoke(receiver, appContext, intent);
                }
            }
        }
    } catch (Exception e) {
        Log.e("WebViewProxy", "Exception while setting Proxy : " + e.getMessage());
    }
}

异常告诉我它不会使用任何代理配置。我已经搜索过这个问题,但似乎没有其他人遇到过。我忘记了什么?

10-05 17:18:21.594  13722-13722/com.bachelor.vwm.slidingtabs E/ProxyChangeListener﹕ Using no proxy configuration due to exception:java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference

【问题讨论】:

    标签: android proxy android-webview android-5.0-lollipop


    【解决方案1】:

    检查这个答案:https://stackoverflow.com/a/25485747/365229

    private static boolean setProxyLollipop(WebView webView, String host, int port, String applicationClassName) {
        Log.d(LOG_TAG, "Setting proxy with >= 21 API.");
    
        Context appContext = webView.getContext().getApplicationContext();
        System.setProperty("http.proxyHost", host);
        System.setProperty("http.proxyPort", port + "");
        System.setProperty("https.proxyHost", host);
        System.setProperty("https.proxyPort", port + "");
        try {
            Class applictionCls = Class.forName("android.app.Application");
            Field loadedApkField = applictionCls.getDeclaredField("mLoadedApk");
            loadedApkField.setAccessible(true);
            Object loadedApk = loadedApkField.get(appContext);
            Class loadedApkCls = Class.forName("android.app.LoadedApk");
            Field receiversField = loadedApkCls.getDeclaredField("mReceivers");
            receiversField.setAccessible(true);
            ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk);
            for (Object receiverMap : receivers.values()) {
                for (Object rec : ((ArrayMap) receiverMap).keySet()) {
                    Class clazz = rec.getClass();
                    if (clazz.getName().contains("ProxyChangeListener")) {
                        Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class);
                        Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
                        final String CLASS_NAME = "android.net.ProxyInfo";
                        Class cls = Class.forName(CLASS_NAME);
                        Constructor constructor = cls.getConstructor(String.class, Integer.TYPE, String.class);
                        constructor.setAccessible(true);
                        Object proxyProperties = constructor.newInstance(host, port, null);
                        intent.putExtra("proxy", (Parcelable) proxyProperties);
                        onReceiveMethod.invoke(rec, appContext, intent);
                    }
                }
            }
    
            Log.d(LOG_TAG, "Setting proxy with >= 21 API successful!");
            return true;
        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            String exceptionAsString = sw.toString();
            Log.v(LOG_TAG, e.getMessage());
            Log.v(LOG_TAG, exceptionAsString);
        }
        return false;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-29
      • 1970-01-01
      • 2023-03-11
      • 2015-06-13
      • 2016-08-05
      • 1970-01-01
      • 2016-02-27
      相关资源
      最近更新 更多