没有代理的Android不工作?
我需要在我的应用程序中不设置代理的条件;为此,我使用了以下代码:没有代理的Android不工作?,android,proxy,httpurlconnection,Android,Proxy,Httpurlconnection,我需要在我的应用程序中不设置代理的条件;为此,我使用了以下代码: URL url = null; try { url = new URL(uri.toURL().toString()); } catch (MalformedURLException e3) { e3.printStackTrace(); } try { //client = (HttpURLConnection) url.openConnection(java.net.Proxy.NO_PROXY);
URL url = null;
try {
url = new URL(uri.toURL().toString());
} catch (MalformedURLException e3) {
e3.printStackTrace();
}
try {
//client = (HttpURLConnection) url.openConnection(java.net.Proxy.NO_PROXY);
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.nonProxyHosts",ServerIP);
systemProperties.setProperty( "proxySet", "false" );
systemProperties.setProperty("http.proxyHost","");
systemProperties.setProperty("http.proxyPort","");
URLConnection conn = url.openConnection(Proxy.NO_PROXY);
conn.connect();
} catch (IOException e3) {
e3.printStackTrace();
}
但我得到了网络无法访问的异常
任何帮助 如果我没有误解你的问题。。。当服务器通过WIFI连接时,是否要直接连接到服务器
HttpURLConnection con =null;
URL url = new URL("xxxxx");
boolean isProxy=true;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm!=null){
NetworkInfo ni = cm.getActiveNetworkInfo();
if(ni!=null){
if(! ni.getTypeName().equals("WIFI")){
isProxy=false;
}
if(isProxy){
Proxy proxy=new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress(android.net.Proxy.getDefaultHost(),android.net.Proxy.getDefaultPort()));
con = (HttpURLConnection) url.openConnection(proxy);
}else{
con = (HttpURLConnection) url.openConnection();
}
}
}
p、 请注意,上面的代码片段可能会遗漏一些错误处理。谢谢;) 你能详细说明你的问题吗?情况如何?它是在模拟器上还是在设备上失败?或者两者都有?您的网络环境是什么?您好,我的目标是使用代理和非代理环境连接到服务器。我在android设备上运行应用程序,通过wifi进行连接。