3

I'm trying to access the Internet through my Android app in Lollipop. The app has a WebView. For some reason the WebView won't let me access the Internet when I'm behind my companies' proxy (I can access the desired page through the Android Chrome browser though). After searching two days for a solution I decided to ask for a solution here. Below is the method which according to some other posts should fix the issue, but it doesn't. It throws an exception.

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

The Exception tells me that it won't use any Proxy Configuration. I've have searched that issue but it seems nobody else encountered it. What have I forgotten?

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
Floern
  • 33,559
  • 24
  • 104
  • 119
Jochen Österreicher
  • 683
  • 2
  • 11
  • 25

1 Answers1

0

check this answer: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;
}
Behrouz.M
  • 3,445
  • 6
  • 37
  • 64