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