我尝试在我的 Android 应用程序中创建 http 代理。我在方法中使用此代码onCreate()
:
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.0.2.2", 8080));
HttpURLConnection e = (HttpURLConnection)url.openConnection(proxy);
e.setReadTimeout(10000);
e.setConnectTimeout(15000);
e.setRequestMethod("GET");
e.connect();
我的应用程序抛出异常:
I/O exception: java.net.SocketTimeoutException: failed to connect to /10.0.2.2 (port 8080) after 15000ms
然后我尝试使用LittleProxy创建 http 代理服务器:
compile group: 'org.littleshoot', name: 'littleproxy', version: '1.0.0-beta7'
public class MyHttpProxyServer {
org.littleshoot.proxy.HttpProxyServer server;
public MyHttpProxyServer(){
server = DefaultHttpProxyServer.bootstrap().withAddress(InetSocketAddress.createUnresolved("10.0.2.2", 8080)).start();
}
}
我将此行添加到 gradle.build 文件中,否则 bc gradle 不想构建应用程序:
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
}
在这里我的模拟器拒绝启动应用程序:
Failure [INSTALL_FAILED_NO_MATCHING_ABIS]
当我评论所有 LittleProxy 导入时 - 模拟器再次正常工作。所以我怀疑 LittleProxy 是否与 Android 兼容?另一方面,我看到人们以前使用过它