在android1.6之前的api中有有直接的方法来对webview设置代码,但是在之后的API中去掉了。所以只能寻求别的方法来设置代码。
下面代码是用反射原理来对webview设置代理:
public static boolean setProxy(Context ctx, String host, int port) {
boolean ret = false;
try {
Log.d("WebViewProxySettings", "setProxy defaulthost="+host+"port="+port);
Object requestQueueObject = getRequestQueue(ctx);
if (requestQueueObject != null) {
//Create Proxy config object and set it into request Q
HttpHost httpHost = new HttpHost(host, port, "http");
setDeclaredField(requestQueueObject, "mProxyHost", httpHost);
ret = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
public static String getProxyHostname(Context ctx){
String