通过反射手动代理在Android中 [英] Manual Proxy in Android through reflection
问题描述
我想设置的SSID,代理,通过ipsetting在Android中的反射和IM成功的SSID和通过反射的android简单的IP设置,我的问题是,我想通过反射programtically设置代理服务器设置而大部分的codeI瞪大眼睛都说实施静态和NONE 选项,但设备的I HV代理选项 NONE和手动是两个一样的吗?下面是$ C $下我的代理PLZ建议正是我应该改变工作手动代理实现:
公共静态无效setWifiProxySettings(WifiConfiguration配置,
WifiSetting wifiSetting){
尝试 {
对象linkProperties = getfield命令(配置linkProperties);
如果(空== linkProperties)
返回;
类proxyPropertiesClass =类
.forName(android.net.ProxyProperties);
类[] setHttpProxyParams =新类[1];
setHttpProxyParams [0] = proxyPropertiesClass;
类lpClass =的Class.forName(android.net.LinkProperties);
方法setHttpProxy = lpClass.getDeclaredMethod(setHttpProxy
setHttpProxyParams);
setHttpProxy.setAccessible(真正的);
类[] proxyPropertiesCtorParamTypes =新等级[3];
proxyPropertiesCtorParamTypes [0] =为String.class;
proxyPropertiesCtorParamTypes [1] = int.class;
proxyPropertiesCtorParamTypes [2] =为String.class;
构造proxyPropertiesCtor = proxyPropertiesClass
.getConstructor(proxyPropertiesCtorParamTypes);
[对象] proxyPropertiesCtorParams =新的对象[3]。
URL proxyUrl =新的URL(wifiSetting.getProxyHostName());
proxyPropertiesCtorParams [0] = proxyUrl.getHost();
proxyPropertiesCtorParams [1] = proxyUrl.getPort();
proxyPropertiesCtorParams [2] =无效;
对象proxySettings = proxyPropertiesCtor
.newInstance(proxyPropertiesCtorParams);
[对象] PARAMS =新的对象[1];
PARAMS [0] = proxySettings;
setHttpProxy.invoke(linkProperties,则params);
setProxySettings(静态,配置);
}赶上(例外五){
e.printStackTrace();
}
}
公共静态无效setProxySettings(字符串赋值,WifiConfiguration wifiConf)
抛出SecurityException异常,抛出:IllegalArgumentException,
NoSuchFieldException,IllegalAccessException {
setEnumField(wifiConf,分配,proxySettings);
}
我已经通过反射写在下面的Android的API代理:
/ **
*这个API是用来设置IP和使用下面的代理服务器设置
*支持方法
*
* @参数wifiSetting
* @参数wifiConf
* /
公共静态无效setWifiSettings(WifiSetting wifiSetting,
WifiConfiguration wifiConf){
//检查IP设置是静态的自定义IP设置
// CONFIGRATION
如果(静态.equals(wifiSetting.getIpSetting())){
setIpSettings(wifiSetting,wifiConf);
}
//如果Proxy功能设置它的自定义代理服务器设置
如果(wifiSetting.getIsProxyEnabled()==真){
setWifiProxySettings(wifiSetting,wifiConf);
}
}
/ **
*这个API是用来设置IP
*
* @参数wifiSetting
* @参数wifiConf
* /
私有静态无效setIpSettings(WifiSetting wifiSetting,
WifiConfiguration wifiConf){
尝试 {
setEnumField(wifiConf,静,ipAssignment);
setIpAddress(wifiSetting.getIpAddress(),
wifiSetting.getNetwork prefixLength(),wifiConf);
setGateway(wifiSetting.getGateway(),wifiConf);
setDNS(wifiSetting,wifiConf);
}赶上(例外五){
e.printStackTrace();
}
}
/ **
*此API用于WiFi设置设置Ip地址
*
* @参数ipAddres
*参数prefixLength
* /
私有静态无效setIpAddress(字符串ip地址,
INT网络prefixLength,WifiConfiguration wifiConf)
抛出SecurityException异常,抛出:IllegalArgumentException,
NoSuchFieldException,IllegalAccessException,
NoSuchMethodException,ClassNotFoundException的,
InstantiationException,的InvocationTargetException {
尝试 {
如果(TextUtils.isEmpty(ip地址)){
抛出新抛出:IllegalArgumentException(
需要的参数不能为空);
}
InetAddress类的InetAddr = NULL;
类networkUtils =的Class.forName(android.net.NetworkUtils);
方法numericToInetAddress = networkUtils.getDeclaredMethod(
numericToInetAddress,ipAddress.getClass());
的InetAddr =(InetAddress类)numericToInetAddress.invoke(空,
IP地址);
如果(网络prefixLength℃,||网络prefixLength> 32){
抛出新抛出:IllegalArgumentException(
无效的网络prefixLength参数);
}
对象linkProperties =使用getFieldValue(wifiConf.getClass(),
wifiConf,linkProperties);
如果(linkProperties == NULL){
抛出新抛出:IllegalArgumentException(
需要的参数不能为空);
}
类<> laClass =的Class.forName(android.net.LinkAddress);
构造函数和LT;> laConstructor = laClass
.getConstructor(新类[] {InetAddress.class,
int.class});
对象linkAddress上= laConstructor.newInstance(InetAddr代表,
网络prefixLength);
类<> setIpAddress =类
.forName(android.net.LinkProperties);
布尔结果=(布尔)invokeDeclaredMethod(setIpAddress,
linkProperties,addLinkAddress
新等级[] {} laClass,新的对象[] {linkAddress上});
}赶上(例外五){
e.printStackTrace();
}
}
/ **
*此API用于wifiConfigration设置网关
*
* @参数网关
* @参数wifiConf
* /
私有静态无效setGateway(字符串网关,
WifiConfiguration wifiConf)抛出SecurityException异常,
抛出:IllegalArgumentException,NoSuchFieldException,
IllegalAccessException,ClassNotFoundException的,
NoSuchMethodException,InstantiationException,
的InvocationTargetException {
尝试 {
如果(TextUtils.isEmpty(网关)){
抛出新抛出:IllegalArgumentException(
需要的参数不能为空);
}
InetAddress类的InetAddr = NULL;
类networkUtils =的Class.forName(android.net.NetworkUtils);
方法numericToInetAddress = networkUtils.getDeclaredMethod(
numericToInetAddress,gateway.getClass());
的InetAddr =(InetAddress类)numericToInetAddress.invoke(空,
网关);
对象linkProperties =使用getFieldValue(wifiConf.getClass(),
wifiConf,linkProperties);
如果(linkProperties == NULL){
抛出新抛出:IllegalArgumentException(
需要的参数不能为空);
}
类routeInfoClass =的Class.forName(android.net.RouteInfo);
构造routeInfoConstructor = routeInfoClass
.getConstructor(新等级[] {InetAddress.class});
对象routeInfo = routeInfoConstructor.newInstance(的InetAddr);
类<> linkPropertiesClass =类
.forName(android.net.LinkProperties);
布尔结果=(布尔)invokeDeclaredMethod(
linkPropertiesClass,linkProperties,addRoute
新等级[] {} routeInfoClass,
新对象[] {routeInfo});
}赶上(例外五){
e.printStackTrace();
}
}
/ **
*此API用于wifiConfigration设置DNS
*
* @参数DNS
* @参数wifiConf
* @throws NoSuchMethodException
* @throws的InvocationTargetException
* /
私有静态无效setDNS(WifiSetting wifiSettings,
WifiConfiguration wifiConf)抛出SecurityException异常,
抛出:IllegalArgumentException,NoSuchFieldException,
IllegalAccessException,NoSuchMethodException,
的InvocationTargetException {
尝试 {
串DNS1 = wifiSettings.getDns1();
串DNS2 = wifiSettings.getDns2();
如果(TextUtils.isEmpty(DNS1)及&安培; TextUtils.isEmpty(DNS2)){
抛出新抛出:IllegalArgumentException(
需要的参数不能为空);
}
类dnsInfo =的Class.forName(android.net.NetworkUtils);
方法setHttpProxy = dnsInfo.getDeclaredMethod(
numericToInetAddress,为String.class);
InetAddress类inetAddressDns1 =(InetAddress类)setHttpProxy
.invoke(NULL,DNS1);
InetAddress类inetAddressDns2 =(InetAddress类)setHttpProxy
.invoke(NULL,DNS2);
对象linkProperties =使用getFieldValue(wifiConf.getClass(),
wifiConf,linkProperties);
如果(linkProperties == NULL){
抛出新抛出:IllegalArgumentException(
需要的参数不能为空);
}
类<> linkPropertiesClass =类
.forName(android.net.LinkProperties);
类<> inetAddressClass =类
.forName(java.net.InetAddress中);
invokeDeclaredMethod(linkPropertiesClass,linkProperties,
addDns,新的等级[] {} inetAddressClass,
新对象[] {inetAddressDns1});
invokeDeclaredMethod(linkPropertiesClass,linkProperties,
addDns,新的等级[] {} inetAddressClass,
新对象[] {inetAddressDns2});
}赶上(ClassNotFoundException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(RemoteException的E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
/ **
*此API用于wifiConfigration设置代理服务器
*
* @参数proxyHostName
* @参数proxyPort
* @参数proxyExceptions
* @参数配置
* /
私有静态无效setWifiProxySettings(WifiSetting wifiSettings,
WifiConfiguration配置){
尝试 {
如果(空==配置){
抛出新抛出:IllegalArgumentException(
需要的参数不能为空);
}
//从无线网络配置中的链接属性
对象linkProperties =使用getFieldValue(config.getClass(),
配置,linkProperties);
如果(空== linkProperties){
抛出新抛出:IllegalArgumentException(
需要的参数不能为空);
}
//获取LinkProperties的setHttpProxy方法
类proxyPropertiesClass =类
.forName(android.net.ProxyProperties);
类[] setHttpProxyParams =新类[1];
setHttpProxyParams [0] = proxyPropertiesClass;
类lpClass =的Class.forName(android.net.LinkProperties);
方法setHttpProxy = lpClass.getDeclaredMethod(setHttpProxy
setHttpProxyParams);
setHttpProxy.setAccessible(真正的);
//获取ProxyProperties构造
类[] proxyPropertiesCtorParamTypes =新等级[3];
proxyPropertiesCtorParamTypes [0] =为String.class;
proxyPropertiesCtorParamTypes [1] = int.class;
proxyPropertiesCtorParamTypes [2] =为String.class;
构造proxyPropertiesCtor = proxyPropertiesClass
.getConstructor(proxyPropertiesCtorParamTypes);
//创建参数的构造函数
[对象] proxyPropertiesCtorParams =新的对象[3]。
proxyPropertiesCtorParams [0] = wifiSettings.getProxyHostName();
proxyPropertiesCtorParams [1] = wifiSettings.getProxyPort();
proxyPropertiesCtorParams [2] = wifiSettings
.getProxyExceptions();
//使用PARAMS创建新对象
对象proxySettings = proxyPropertiesCtor
.newInstance(proxyPropertiesCtorParams);
//通过新的对象来setHttpProxy
[对象] PARAMS =新的对象[1];
PARAMS [0] = proxySettings;
setHttpProxy.invoke(linkProperties,则params);
setEnumField(配置,静,proxySettings);
}赶上(例外五){
e.printStackTrace();
}
}
公共静态对象invokeDeclaredMethod(类<> clazz中,Object对象,
字符串方法名,类<> []的args,对象[] argsValue)
抛出SecurityException异常,NoSuchMethodException,
抛出:IllegalArgumentException,IllegalAccessException,
的InvocationTargetException,RemoteException的{
LogUtil.d(TAG,调用声明的方法+ clazz.getSimpleName()
+。 +方法名);
方法privateMethod = clazz.getDeclaredMethod(方法名,参数);
privateMethod.setAccessible(真正的);
对象result = privateMethod.invoke(对象argsValue);
返回结果;
}
私有静态无效setEnumField(Object对象,字符串值,字符串名称)
抛出SecurityException异常,NoSuchFieldException,
抛出:IllegalArgumentException,IllegalAccessException {
域F = object.getClass()getfield命令(名称)。
f.set(对象,Enum.valueOf((类<枚举>)f.getType(),值));
}
公共静态对象使用getFieldValue(类<> clazz中,Object对象,
字符串字段名){
如果(clazz所== NULL ||字段名== NULL){
抛出新抛出:IllegalArgumentException(
需要的参数不能为空);
}
对象result = NULL;
尝试 {
域F = clazz.getField(字段名);
f.setAccessible(真正的);
结果= f.get(对象);
}赶上(例外五){
e.printStackTrace();
抛出新的RuntimeException(e.getMessage());
}
返回结果;
}
编辑:的API LOLLIPOP [上述API的代理服务器和IP在最新的Andriod大号不会工作]
如果(Build.VERSION.SDK_INT == Build.VERSION_ codeS.LOLLIPOP){
//执行情况的代理设置LOLLIPOP
尝试 {
URL proxyUrl =新的URL(
addNetworkProxyProperties.getHttpProxyUri());
字符串主机= proxyUrl.getHost();
INT portStr = proxyUrl.getPort();
字符串exclusionList = addNetworkProxyProperties
.getExceptions();
构造函数和LT; ProxyInfo>构造函数= ProxyInfo.class
.getConstructor(新类[] {为String.class,int.class,
为String.class});
ProxyInfo mHttpProxy = constructor.newInstance(新对象[] {
主机,portStr,exclusionList});
类ipConfigClass =类
.forName(android.net.IpConfiguration);
对象ipConfigObject = ipConfigClass.newInstance();
方法setHttpProxy = ipConfigClass.getDeclaredMethod(
setHttpProxy,ProxyInfo.class);
setHttpProxy.setAccessible(真正的);
setHttpProxy.invoke(ipConfigObject,mHttpProxy);
方法getHttpProxySettings = ipConfigClass
.getDeclaredMethod(getProxySettings);
getHttpProxySettings.setAccessible(真正的);
方法setProxy = config.getClass()。getDeclaredMethod(
setProxy
getHttpProxySettings.invoke(ipConfigObject).getClass(),
ProxyInfo.class);
setProxy.setAccessible(真正的);
setEnumField(ipConfigObject,静,proxySettings);
setProxy.invoke(配置,
getHttpProxySettings.invoke(ipConfigObject),mHttpProxy);
}赶上(例外五){
/ *
*例外的例外可能是SecurityException异常,
*抛出:IllegalArgumentException,IllegalAccessException,
*的InvocationTargetException,NoSuchMethodException,
* InstantiationException,ClassNotFoundException的,
* NoSuchFieldException,MalformedURLException的
* /
e.printStackTrace();
}
}
I am trying to set ssid,proxy,ipsetting in android through reflection and i m successfully for ssid and brief ip settings in android via reflection , my issue is i want to set proxy settings programtically via reflection and most of the code i goggled say implementation of STATIC and NONE options but devices i hv proxy options as NONE and MANUAL is both same ? below is code for my proxy plz suggest what exactly should i change to work for manual proxy implementation :
public static void setWifiProxySettings(WifiConfiguration config,
WifiSetting wifiSetting) {
try {
Object linkProperties = getField(config, "linkProperties");
if (null == linkProperties)
return;
Class proxyPropertiesClass = Class
.forName("android.net.ProxyProperties");
Class[] setHttpProxyParams = new Class[1];
setHttpProxyParams[0] = proxyPropertiesClass;
Class lpClass = Class.forName("android.net.LinkProperties");
Method setHttpProxy = lpClass.getDeclaredMethod("setHttpProxy",
setHttpProxyParams);
setHttpProxy.setAccessible(true);
Class[] proxyPropertiesCtorParamTypes = new Class[3];
proxyPropertiesCtorParamTypes[0] = String.class;
proxyPropertiesCtorParamTypes[1] = int.class;
proxyPropertiesCtorParamTypes[2] = String.class;
Constructor proxyPropertiesCtor = proxyPropertiesClass
.getConstructor(proxyPropertiesCtorParamTypes);
Object[] proxyPropertiesCtorParams = new Object[3];
URL proxyUrl = new URL(wifiSetting.getProxyHostName());
proxyPropertiesCtorParams[0] = proxyUrl.getHost();
proxyPropertiesCtorParams[1] = proxyUrl.getPort();
proxyPropertiesCtorParams[2] = null;
Object proxySettings = proxyPropertiesCtor
.newInstance(proxyPropertiesCtorParams);
Object[] params = new Object[1];
params[0] = proxySettings;
setHttpProxy.invoke(linkProperties, params);
setProxySettings("STATIC", config);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void setProxySettings(String assign, WifiConfiguration wifiConf)
throws SecurityException, IllegalArgumentException,
NoSuchFieldException, IllegalAccessException {
setEnumField(wifiConf, assign, "proxySettings");
}
I have written below apis for proxy in android via reflection :
/**
* This api is used for setting IP And Proxy Setting using below
* supporting methods
*
* @param wifiSetting
* @param wifiConf
*/
public static void setWifiSettings(WifiSetting wifiSetting,
WifiConfiguration wifiConf) {
// check if ip setting is static for custom ip setting
// configration
if ("STATIC".equals(wifiSetting.getIpSetting())) {
setIpSettings(wifiSetting, wifiConf);
}
// if proxy is enabled set its custom proxy settings
if (wifiSetting.getIsProxyEnabled() == true) {
setWifiProxySettings(wifiSetting, wifiConf);
}
}
/**
* This api is used for setting IP
*
* @param wifiSetting
* @param wifiConf
*/
private static void setIpSettings(WifiSetting wifiSetting,
WifiConfiguration wifiConf) {
try {
setEnumField(wifiConf, "STATIC", "ipAssignment");
setIpAddress(wifiSetting.getIpAddress(),
wifiSetting.getNetworkPrefixLength(), wifiConf);
setGateway(wifiSetting.getGateway(), wifiConf);
setDNS(wifiSetting, wifiConf);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* This api is used for setting IpAddress in wifi settings
*
* @param ipAddres
* @param prefixLength
*/
private static void setIpAddress(String ipAddress,
int networkPrefixLength, WifiConfiguration wifiConf)
throws SecurityException, IllegalArgumentException,
NoSuchFieldException, IllegalAccessException,
NoSuchMethodException, ClassNotFoundException,
InstantiationException, InvocationTargetException {
try {
if (TextUtils.isEmpty(ipAddress)) {
throw new IllegalArgumentException(
"Required argument can not be blank.");
}
InetAddress inetAddr = null;
Class networkUtils = Class.forName("android.net.NetworkUtils");
Method numericToInetAddress = networkUtils.getDeclaredMethod(
"numericToInetAddress", ipAddress.getClass());
inetAddr = (InetAddress) numericToInetAddress.invoke(null,
ipAddress);
if (networkPrefixLength < 0 || networkPrefixLength > 32) {
throw new IllegalArgumentException(
"invalid networkPrefixLength parameter");
}
Object linkProperties = getFieldValue(wifiConf.getClass(),
wifiConf, "linkProperties");
if (linkProperties == null) {
throw new IllegalArgumentException(
"Required argument can not be blank.");
}
Class<?> laClass = Class.forName("android.net.LinkAddress");
Constructor<?> laConstructor = laClass
.getConstructor(new Class[] { InetAddress.class,
int.class });
Object linkAddress = laConstructor.newInstance(inetAddr,
networkPrefixLength);
Class<?> setIpAddress = Class
.forName("android.net.LinkProperties");
Boolean result = (Boolean) invokeDeclaredMethod(setIpAddress,
linkProperties, "addLinkAddress",
new Class[] { laClass }, new Object[] { linkAddress });
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* This api is used for setting gateway in wifiConfigration
*
* @param gateway
* @param wifiConf
*/
private static void setGateway(String gateway,
WifiConfiguration wifiConf) throws SecurityException,
IllegalArgumentException, NoSuchFieldException,
IllegalAccessException, ClassNotFoundException,
NoSuchMethodException, InstantiationException,
InvocationTargetException {
try {
if (TextUtils.isEmpty(gateway)) {
throw new IllegalArgumentException(
"Required argument can not be blank.");
}
InetAddress inetAddr = null;
Class networkUtils = Class.forName("android.net.NetworkUtils");
Method numericToInetAddress = networkUtils.getDeclaredMethod(
"numericToInetAddress", gateway.getClass());
inetAddr = (InetAddress) numericToInetAddress.invoke(null,
gateway);
Object linkProperties = getFieldValue(wifiConf.getClass(),
wifiConf, "linkProperties");
if (linkProperties == null) {
throw new IllegalArgumentException(
"Required argument can not be blank.");
}
Class routeInfoClass = Class.forName("android.net.RouteInfo");
Constructor routeInfoConstructor = routeInfoClass
.getConstructor(new Class[] { InetAddress.class });
Object routeInfo = routeInfoConstructor.newInstance(inetAddr);
Class<?> linkPropertiesClass = Class
.forName("android.net.LinkProperties");
Boolean result = (Boolean) invokeDeclaredMethod(
linkPropertiesClass, linkProperties, "addRoute",
new Class[] { routeInfoClass },
new Object[] { routeInfo });
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* This api is used for setting DNS in wifiConfigration
*
* @param dns
* @param wifiConf
* @throws NoSuchMethodException
* @throws InvocationTargetException
*/
private static void setDNS(WifiSetting wifiSettings,
WifiConfiguration wifiConf) throws SecurityException,
IllegalArgumentException, NoSuchFieldException,
IllegalAccessException, NoSuchMethodException,
InvocationTargetException {
try {
String dns1 = wifiSettings.getDns1();
String dns2 = wifiSettings.getDns2();
if (TextUtils.isEmpty(dns1) && TextUtils.isEmpty(dns2)) {
throw new IllegalArgumentException(
"Required argument can not be blank.");
}
Class dnsInfo = Class.forName("android.net.NetworkUtils");
Method setHttpProxy = dnsInfo.getDeclaredMethod(
"numericToInetAddress", String.class);
InetAddress inetAddressDns1 = (InetAddress) setHttpProxy
.invoke(null, dns1);
InetAddress inetAddressDns2 = (InetAddress) setHttpProxy
.invoke(null, dns2);
Object linkProperties = getFieldValue(wifiConf.getClass(),
wifiConf, "linkProperties");
if (linkProperties == null) {
throw new IllegalArgumentException(
"Required argument can not be blank.");
}
Class<?> linkPropertiesClass = Class
.forName("android.net.LinkProperties");
Class<?> inetAddressClass = Class
.forName("java.net.InetAddress");
invokeDeclaredMethod(linkPropertiesClass, linkProperties,
"addDns", new Class[] { inetAddressClass },
new Object[] { inetAddressDns1 });
invokeDeclaredMethod(linkPropertiesClass, linkProperties,
"addDns", new Class[] { inetAddressClass },
new Object[] { inetAddressDns2 });
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* This api is used for setting Proxy in wifiConfigration
*
* @param proxyHostName
* @param proxyPort
* @param proxyExceptions
* @param config
*/
private static void setWifiProxySettings(WifiSetting wifiSettings,
WifiConfiguration config) {
try {
if (null == config) {
throw new IllegalArgumentException(
"Required argument can not be blank.");
}
// get the link properties from the wifi configuration
Object linkProperties = getFieldValue(config.getClass(),
config, "linkProperties");
if (null == linkProperties) {
throw new IllegalArgumentException(
"Required argument can not be blank.");
}
// get the setHttpProxy method for LinkProperties
Class proxyPropertiesClass = Class
.forName("android.net.ProxyProperties");
Class[] setHttpProxyParams = new Class[1];
setHttpProxyParams[0] = proxyPropertiesClass;
Class lpClass = Class.forName("android.net.LinkProperties");
Method setHttpProxy = lpClass.getDeclaredMethod("setHttpProxy",
setHttpProxyParams);
setHttpProxy.setAccessible(true);
// get ProxyProperties constructor
Class[] proxyPropertiesCtorParamTypes = new Class[3];
proxyPropertiesCtorParamTypes[0] = String.class;
proxyPropertiesCtorParamTypes[1] = int.class;
proxyPropertiesCtorParamTypes[2] = String.class;
Constructor proxyPropertiesCtor = proxyPropertiesClass
.getConstructor(proxyPropertiesCtorParamTypes);
// create the parameters for the constructor
Object[] proxyPropertiesCtorParams = new Object[3];
proxyPropertiesCtorParams[0] = wifiSettings.getProxyHostName();
proxyPropertiesCtorParams[1] = wifiSettings.getProxyPort();
proxyPropertiesCtorParams[2] = wifiSettings
.getProxyExceptions();
// create a new object using the params
Object proxySettings = proxyPropertiesCtor
.newInstance(proxyPropertiesCtorParams);
// pass the new object to setHttpProxy
Object[] params = new Object[1];
params[0] = proxySettings;
setHttpProxy.invoke(linkProperties, params);
setEnumField(config, "STATIC", "proxySettings");
} catch (Exception e) {
e.printStackTrace();
}
}
public static Object invokeDeclaredMethod(Class<?> clazz, Object object,
String methodName, Class<?>[] args, Object[] argsValue)
throws SecurityException, NoSuchMethodException,
IllegalArgumentException, IllegalAccessException,
InvocationTargetException, RemoteException {
LogUtil.d(TAG, "Invoking declared method " + clazz.getSimpleName()
+ "." + methodName);
Method privateMethod = clazz.getDeclaredMethod(methodName, args);
privateMethod.setAccessible(true);
Object result = privateMethod.invoke(object, argsValue);
return result;
}
private static void setEnumField(Object object, String value, String name)
throws SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException {
Field f = object.getClass().getField(name);
f.set(object, Enum.valueOf((Class<Enum>) f.getType(), value));
}
public static Object getFieldValue(Class<?> clazz, Object object,
String fieldName) {
if (clazz == null || fieldName == null) {
throw new IllegalArgumentException(
"Required argument can not be blank.");
}
Object result = null;
try {
Field f = clazz.getField(fieldName);
f.setAccessible(true);
result = f.get(object);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
return result;
}
EDIT : API FOR LOLLIPOP [the above api for proxy and ip wont work in latest Andriod L ]
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
// Implementation for Proxy setting for LOLLIPOP
try {
URL proxyUrl = new URL(
addNetworkProxyProperties.getHttpProxyUri());
String host = proxyUrl.getHost();
int portStr = proxyUrl.getPort();
String exclusionList = addNetworkProxyProperties
.getExceptions();
Constructor<ProxyInfo> constructor = ProxyInfo.class
.getConstructor(new Class[] { String.class, int.class,
String.class });
ProxyInfo mHttpProxy = constructor.newInstance(new Object[] {
host, portStr, exclusionList });
Class ipConfigClass = Class
.forName("android.net.IpConfiguration");
Object ipConfigObject = ipConfigClass.newInstance();
Method setHttpProxy = ipConfigClass.getDeclaredMethod(
"setHttpProxy", ProxyInfo.class);
setHttpProxy.setAccessible(true);
setHttpProxy.invoke(ipConfigObject, mHttpProxy);
Method getHttpProxySettings = ipConfigClass
.getDeclaredMethod("getProxySettings");
getHttpProxySettings.setAccessible(true);
Method setProxy = config.getClass().getDeclaredMethod(
"setProxy",
getHttpProxySettings.invoke(ipConfigObject).getClass(),
ProxyInfo.class);
setProxy.setAccessible(true);
setEnumField(ipConfigObject, "STATIC", "proxySettings");
setProxy.invoke(config,
getHttpProxySettings.invoke(ipConfigObject), mHttpProxy);
} catch (Exception e) {
/*
* Excepted exceptions may be SecurityException,
* IllegalArgumentException, IllegalAccessException,
* InvocationTargetException, NoSuchMethodException,
* InstantiationException, ClassNotFoundException,
* NoSuchFieldException, MalformedURLException
*/
e.printStackTrace();
}
}
这篇关于通过反射手动代理在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!