问题描述
我知道如何手动设置代理服务器,并把它用在我的WebView。
I know how to setting proxy manually and to use it in my WebView.
设置 - >无线网络 - >移动网络 - >接入点名 - > telkila。现在,输入代理服务器地址和端口(这将是80)。WebView.enablePlatformNotifications();
Settings -> Wireless Networks ->mobile networks-> access point names->telkila. Now enter the proxy server address and port (which will be 80).WebView.enablePlatformNotifications();
不过,我可以设置代理从code设定?所以,我的用户没有手动设置?
But can i set the proxy setting from code?So my user didn't have to set manually?
感谢
推荐答案
有以编程方式更改您的WebView代理设置不合法的方式。但它可以使用Java反射从android.net.http.RequestQueue类更改mProxyHost值。它是私有的值,也没有设置器,所以反射似乎是唯一的可能的变型。我用它在我的项目和它的作品。下面是我的方法的示例:
There is no legal way to change your webview proxy settings programmatically. But it's possible to use java reflection to change mProxyHost value from android.net.http.RequestQueue class. It's private value and there is no setters for it, so reflection seems to be the only possible variant. I used it in my project and it works. Here is the sample of my method:
private boolean setProxyHostField(HttpHost proxyServer) {
// Getting network
Class networkClass = null;
Object network = null;
try {
networkClass = Class.forName("android.webkit.Network");
Field networkField = networkClass.getDeclaredField("sNetwork");
network = getFieldValueSafely(networkField, null);
} catch (Exception ex) {
Log.e(ProxyManager.class.getName(), "error getting network");
return false;
}
if (network == null) {
Log.e(ProxyManager.class.getName(), "error getting network : null");
return false;
}
Object requestQueue = null;
try {
Field requestQueueField = networkClass
.getDeclaredField("mRequestQueue");
requestQueue = getFieldValueSafely(requestQueueField, network);
} catch (Exception ex) {
Log.e(ProxyManager.class.getName(), "error getting field value");
return false;
}
if (requestQueue == null) {
Log.e(ProxyManager.class.getName(), "Request queue is null");
return false;
}
Field proxyHostField = null;
try {
Class requestQueueClass = Class.forName("android.net.http.RequestQueue");
proxyHostField = requestQueueClass
.getDeclaredField("mProxyHost");
} catch (Exception ex) {
Log.e(ProxyManager.class.getName(), "error getting proxy host field");
return false;
}
synchronized (synchronizer) {
boolean temp = proxyHostField.isAccessible();
try {
proxyHostField.setAccessible(true);
proxyHostField.set(requestQueue, proxyServer);
} catch (Exception ex) {
Log.e(ProxyManager.class.getName(), "error setting proxy host");
} finally {
proxyHostField.setAccessible(temp);
}
}
return true;
}
private Object getFieldValueSafely(Field field, Object classInstance) throws IllegalArgumentException, IllegalAccessException {
boolean oldAccessibleValue = field.isAccessible();
field.setAccessible(true);
Object result = field.get(classInstance);
field.setAccessible(oldAccessibleValue);
return result;
}
这篇关于的WebView安卓代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!