我的 Android 应用程序在端口 24783 上运行它自己的网络服务器。
当我在本地无线网络中时,我可以连接到它。我在手机 chrome 浏览器中输入本地 wifi ip 和端口,然后网站出现。但是,当我离线时(但服务器在我的手机上本地运行),我无法连接到 127.0.0.1:24783,此外,当我仅通过 4G 连接时,我无法连接到 127.0.0.1:24783要么是因为所有流量都通过我的运营商代理,并且不允许我连接 127.0.0.1
有没有办法告诉手机对于 127.0.0.1 它不应该与代理通信?那 127.0.0.1 总是“本地的”?我很高兴使用我自己的 webview。
更新:
原来这只是一个 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);