Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android without Proxy not working?

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!!

like image 202
info Avatar asked Jul 16 '12 06:07

info


People also ask

How do I disable proxy on emulator?

With the emulator open, click More , and then click Settings and Proxy. From here, you can define your own HTTP proxy settings.


1 Answers

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

like image 142
hungr Avatar answered Sep 29 '22 18:09

hungr