android - http post request get SocketTimeoutException -
i'm trying configure http post request, sockettimeoutexception times. know wrong code?
int timeout_millisec = 60000; // = 60 seconds httpparams httpparams = new basichttpparams(); httpconnectionparams.setconnectiontimeout(httpparams, timeout_millisec); httpconnectionparams.setsotimeout(httpparams, timeout_millisec); client = new defaulthttpclient(httpparams); stringbuilder builder = new stringbuilder(url_string); httppost request = new httppost(builder.tostring()); request.setentity(new bytearrayentity(jobj.tostring().getbytes("utf8"))); httpresponse response = client.execute(request);
refer code. understand handling basic httpclient exception. try {
// check request method if (method == "post") { // request method post // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(params)); // new httpparams httpparameters = httppost.getparams(); // set timeout in milliseconds until connection // established. int timeoutconnection = 10000; httpconnectionparams.setconnectiontimeout(httpparameters, timeoutconnection); // set default socket timeout (so_timeout) // in milliseconds timeout waiting data. int timeoutsocket = 10000; httpconnectionparams .setsotimeout(httpparameters, timeoutsocket); // new httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } else if (method == "get") { // request method defaulthttpclient httpclient = new defaulthttpclient(); string paramstring = urlencodedutils.format(params, "utf-8"); url += "?" + paramstring; httpget httpget = new httpget(url); // new httpparams httpparameters = httpget.getparams(); // set timeout in milliseconds until connection // established. int timeoutconnection = 10000; httpconnectionparams.setconnectiontimeout(httpparameters, timeoutconnection); // set default socket timeout (so_timeout) // in milliseconds timeout waiting data. int timeoutsocket = 10000; httpconnectionparams .setsotimeout(httpparameters, timeoutsocket); // new httpresponse httpresponse = httpclient.execute(httpget); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } } catch (unsupportedencodingexception e) { throw new exception("unsupported encoding error."); } catch (clientprotocolexception e) { throw new exception("client protocol error."); } catch (sockettimeoutexception e) { throw new exception("sorry, socket timeout."); } catch (connecttimeoutexception e) { throw new exception("sorry, connection timeout."); } catch (ioexception e) { throw new exception("i/o error(may server down)."); }
Comments
Post a Comment