I need the condition of setting no proxy in my application; for that 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 network unreachable exception!!
Any help!!
With the emulator open, click More , and then click Settings and Proxy. From here, you can define your own HTTP proxy settings.
If I do not misunderstand your question... You want to connect to server directly when it is connecting 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();
        }
    }
}
p.s. Please note that the code snippet above may miss some error handling. Thanks ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With