我的Android应用程序在端口24783上运行它自己的网络服务器。
当我在本地无线网络时,我可以连接到它。我输入本地wifi ip和手机Chrome浏览器中的端口,然后出现网站。但是,当我离线(但服务器在我的手机上本地运行)时,我无法连接到127.0.0.1:24783而且当我只通过4G连接时我无法连接到127.0.0.1:24783或者因为所有流量都超过我的运营商代理并且不允许我在127.0.0.1上连接
有没有办法告诉手机127.0.0.1它不应该与代理通话?那127.0.0.1总是“本地的”?我很高兴使用自己的网页浏览。
更新
原来这只是一个KitKat问题。
我还没有找到解决办法,但离得更近了。
解决方案仅在我打开应用程序时起作用,使用后退按钮将其关闭并再次重新打开:
package com.ndream.console;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.net.Proxy;
import android.os.Build;
import android.os.Parcelable;
import android.util.ArrayMap;
import android.util.Log;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Created by andrin on 15/01/15.
*/
public class ProxyUtils {
@TargetApi(Build.VERSION_CODES.KITKAT)
public static void setKitKatWebViewProxy(Context appContext, String host, int port) {
if (Build.VERSION.SDK_INT != 19) {
return;
}
Log.i("Console", "PROXY MANUALLY SET");
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);
/*********** optional, may be need in future *************/
final String CLASS_NAME = "android.net.ProxyProperties";
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);
/*********** optional, may be need in future *************/
onReceiveMethod.invoke(rec, appContext, intent);
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
然后在启动时我打电话:
ProxyUtils.setKitKatWebViewProxy(getApplicationContext(), "", 0);