[Java] Come resettare il proxy in Android WebView per le versioni Android inferiori a KitKat?


Answers

Question

Sto usando i seguenti 2 metodi per impostare il proxy nel mio Android WebView per le versioni di Android ICS e JB. Ma non sono in grado di ripristinare / rimuovere questo proxy per queste 2 versioni. In che modo il proxy impostato con questi metodi può essere reimpostato / rimosso?

Per ICS:

private static boolean setProxyICS(WebView webview, String host, int port) {
        try {
            Class jwcjb = Class.forName("android.webkit.JWebCoreJavaBridge");

            Class params[] = new Class[1];
            params[0] = Class.forName("android.net.ProxyProperties");

            Method updateProxyInstance = jwcjb.getDeclaredMethod("updateProxy",
                    params);

            Class wv = Class.forName("android.webkit.WebView");

            Field mWebViewCoreField = wv.getDeclaredField("mWebViewCore");

            Object mWebViewCoreFieldInstance = getFieldValueSafely(
                    mWebViewCoreField, webview);

            Class wvc = Class.forName("android.webkit.WebViewCore");

            Field mBrowserFrameField = wvc.getDeclaredField("mBrowserFrame");

            Object mBrowserFrame = getFieldValueSafely(mBrowserFrameField,
                    mWebViewCoreFieldInstance);

            Class bf = Class.forName("android.webkit.BrowserFrame");

            Field sJavaBridgeField = bf.getDeclaredField("sJavaBridge");

            Object sJavaBridge = getFieldValueSafely(sJavaBridgeField,
                    mBrowserFrame);

            Class ppclass = Class.forName("android.net.ProxyProperties");

            Class pparams[] = new Class[3];
            pparams[0] = String.class;
            pparams[1] = int.class;
            pparams[2] = String.class;

            Constructor ppcont = ppclass.getConstructor(pparams);

            updateProxyInstance.invoke(sJavaBridge, ppcont.newInstance(host, port, null));

        } catch (Exception e) {
            return false;
        }
        return true;
    }

e per JellyBean:

private static boolean setProxyJB(WebView webview, String host, int port) {
        try {
            Class wvcClass = Class.forName("android.webkit.WebViewClassic");
            Class wvParams[] = new Class[1];
            wvParams[0] = Class.forName("android.webkit.WebView");
            Method fromWebView = wvcClass.getDeclaredMethod("fromWebView",
                    wvParams);
            Object webViewClassic = fromWebView.invoke(null, webview);

            Class wv = Class.forName("android.webkit.WebViewClassic");
            Field mWebViewCoreField = wv.getDeclaredField("mWebViewCore");
            Object mWebViewCoreFieldInstance = getFieldValueSafely(
                    mWebViewCoreField, webViewClassic);

            Class wvc = Class.forName("android.webkit.WebViewCore");
            Field mBrowserFrameField = wvc.getDeclaredField("mBrowserFrame");
            Object mBrowserFrame = getFieldValueSafely(mBrowserFrameField,
                    mWebViewCoreFieldInstance);

            Class bf = Class.forName("android.webkit.BrowserFrame");
            Field sJavaBridgeField = bf.getDeclaredField("sJavaBridge");
            Object sJavaBridge = getFieldValueSafely(sJavaBridgeField,
                    mBrowserFrame);

            Class ppclass = Class.forName("android.net.ProxyProperties");
            Class pparams[] = new Class[3];
            pparams[0] = String.class;
            pparams[1] = int.class;
            pparams[2] = String.class;
            Constructor ppcont = ppclass.getConstructor(pparams);

            Class jwcjb = Class.forName("android.webkit.JWebCoreJavaBridge");
            Class params[] = new Class[1];
            params[0] = Class.forName("android.net.ProxyProperties");
            Method updateProxyInstance = jwcjb.getDeclaredMethod("updateProxy", params);

            updateProxyInstance.invoke(sJavaBridge, ppcont.newInstance(host, port, null));
        } catch (Exception e) {
            return false;
        }
        return true;
    }