Android does not work without a proxy?

I need a condition for installing a proxy server in my application; For this, I used the following code:

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(); } 

But I got an unreachable exception on the net !!

Any help!

+6
source share
1 answer

If I do not understand your question correctly ... Do you want to connect directly to the server when it connects via 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(); } } } 

ps Please note that the code snippet above may skip some error handling. Thanks;)

+5
source

Source: https://habr.com/ru/post/920560/


All Articles