android - HttpURLConnection setConnectTimeout not working -


i using httpurlconnection communicate server in app. find out if there bad connection of android device, app hanging long time , bad user experience. use both setconnecttimeout() , setreadtimeout() seems not working. here code below:

        url url = null;         httpurlconnection connection = null;         inputstreamreader ins = null;            try {             url = new url("xxxxxxxxxxxxxxxxx");             connection = (httpurlconnection) url.openconnection();             connection.setconnecttimeout(10000);             connection.setreadtimeout(10000);             ins = new inputstreamreader(connection.getinputstream());               bufferedreader bufferedreader = new bufferedreader(ins);             stringbuffer strbuffer = new stringbuffer();             string line = null;             while ((line = bufferedreader.readline()) != null) {                 strbuffer.append(line);             }                    result = strbuffer.tostring();                    line = null;             strbuffer = null;             bufferedreader = null;         } catch (exception e) {             e.printstacktrace();         } {             if (connection != null) {                 connection.disconnect();                 connection = null;             }             if (ins != null) {                 try {                     ins.close();                 } catch (ioexception e) {                     e.printstacktrace();                 }                 ins = null;             }             url = null;         } 

from code can see use both setconnecttimeout() , setreadtimeout() when have poor internet connect. however, still hang out long time(more 30 seconds or more) , show such logs in end:

    07-17 13:27:47.200: w/system.err(18005): java.net.unknownhostexception: unable resolve host "server.ibaobeimao.com": no address associated hostname     07-17 13:27:47.200: w/system.err(18005):    @ java.net.inetaddress.lookuphostbyname(inetaddress.java:424)     07-17 13:27:47.200: w/system.err(18005):    @ java.net.inetaddress.getallbynameimpl(inetaddress.java:236)     07-17 13:27:47.200: w/system.err(18005):    @ java.net.inetaddress.getallbyname(inetaddress.java:214)     07-17 13:27:47.200: w/system.err(18005):    @ libcore.net.http.httpconnection.<init>(httpconnection.java:70)     07-17 13:27:47.203: w/system.err(18005):    @ libcore.net.http.httpconnection.<init>(httpconnection.java:50)     07-17 13:27:47.207: w/system.err(18005):    @ libcore.net.http.httpconnection$address.connect(httpconnection.java:340)     07-17 13:27:47.207: w/system.err(18005):    @ libcore.net.http.httpconnectionpool.get(httpconnectionpool.java:87)     07-17 13:27:47.207: w/system.err(18005):    @ libcore.net.http.httpconnection.connect(httpconnection.java:128)     07-17 13:27:47.207: w/system.err(18005):    @ libcore.net.http.httpengine.opensocketconnection(httpengine.java:316)     07-17 13:27:47.207: w/system.err(18005):    @ libcore.net.http.httpengine.connect(httpengine.java:311)     07-17 13:27:47.210: w/system.err(18005):    @ libcore.net.http.httpengine.sendsocketrequest(httpengine.java:290)     07-17 13:27:47.210: w/system.err(18005):    @ libcore.net.http.httpengine.sendrequest(httpengine.java:240)     07-17 13:27:47.210: w/system.err(18005):    @ libcore.net.http.httpurlconnectionimpl.getresponse(httpurlconnectionimpl.java:282)     07-17 13:27:47.213: w/system.err(18005):    @ libcore.net.http.httpurlconnectionimpl.getinputstream(httpurlconnectionimpl.java:177)     07-17 13:27:47.213: w/system.err(18005):    @ utils.rpc.httprequest(rpc.java:49)     07-17 13:27:47.213: w/system.err(18005):    @ utils.utils$11.run(utils.java:1154)     07-17 13:27:47.213: w/system.err(18005):    @ java.lang.thread.run(thread.java:856)     07-17 13:27:47.213: w/system.err(18005): caused by: libcore.io.gaiexception: getaddrinfo failed: eai_nodata (no address associated hostname)     07-17 13:27:47.217: w/system.err(18005):    @ libcore.io.posix.getaddrinfo(native method)     07-17 13:27:47.217: w/system.err(18005):    @ libcore.io.forwardingos.getaddrinfo(forwardingos.java:60)     07-17 13:27:47.217: w/system.err(18005):    @ java.net.inetaddress.lookuphostbyname(inetaddress.java:405)     07-17 13:27:47.217: w/system.err(18005):    ... 16 more     07-17 13:27:47.217: w/system.err(18005): caused by: libcore.io.errnoexception: getaddrinfo failed: etimedout (connection timed out)     07-17 13:27:47.220: w/system.err(18005):    ... 19 more 

so, can set length of time httpurlconnection timeout ? why setconnecttimeout() , setreadtimeout() not working?

here try uses ping in android, unkonwn reason, still does't work, lay out method used:

public static final boolean ping() {      string result = null;     try {         string ip = "202.108.22.5"; // ping address         process p = runtime.getruntime().exec("/system/bin/ping -c 1 -w 10 " + ip); // ping 3 times          inputstream input = p.getinputstream();         bufferedreader in = new bufferedreader(new inputstreamreader(input));         stringbuffer stringbuffer = new stringbuffer();         string content = "";         while ((content = in.readline()) != null) {             stringbuffer.append(content);         }          // ping's situation         int status = p.waitfor();         if (status == 0) {             result = "success";             return true;         } else {             result = "failed";         }     } catch (ioexception e) {         result = "ioexception";     } catch (interruptedexception e) {         result = "interruptedexception";     } {         log.d("----result---", "result = " + result);     }     return false; } 

but result false time whenever in connection internet or not. give me more advice?

your problem @ lower networking layer unfortunately. parameters come play when requesting http. stuck @ resolving dns. sequence of event:

  • resolve dns
  • establish tcp connection
  • send http request
  • receive http response

the thing can think of deal connection check opening connection google gen_204.


Comments