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;)
source share