httpclient - set timeout in httprequest android -
i using following code data server http request.
httpclient client = new defaulthttpclient(); string url = urlgenerator(); stringbuilder url = new stringbuilder(url); httpget = new httpget(url.tostring()); httpresponse response = client.execute(get); int status = response.getstatusline().getstatuscode(); if(status == 200){ ... }
its working fine.but in case if phone connected wifi or gprs 3g internet not working or internet connection not there, want use timeout feature in above code.
say after 3 sec want show timeout please try again.. how do that. in case of time out want show text in textviw connection timeout .. how do please help
you can follows:
try{ httpget httpget = new httpget(url); httpparams httpparameters = new basichttpparams(); // set timeout in milliseconds until connection established. // default value zero, means timeout not used. int timeoutconnection = 4000; httpconnectionparams.setconnectiontimeout(httpparameters, timeoutconnection); // set default socket timeout (so_timeout) // in milliseconds timeout waiting data. int timeoutsocket = 6000; httpconnectionparams.setsotimeout(httpparameters, timeoutsocket); defaulthttpclient httpclient = new defaulthttpclient(httpparameters); httpresponse response = httpclient.execute(httpget); } catch (connecttimeoutexception e) { //here connection timeout excepion toast.maketext(xyz.this, "your connection timedout", 10000).show(); }
Comments
Post a Comment