Android-defaulthttpclient set timeout.

Source: Internet
Author: User

In Android Defaulthttpclient, the timeout setting requires calling the Httpconnectionparams.setconnectiontimeout method, such as (31~35 line code for the following code):

Ref: Timeout in defaulthttpclient Class Android

/** the method of sending the POST request * @param the key-value pair of the params request parameter. * @param URL * @param encoding * @param setTimeout identifies whether the connection timeout is set. * @param connectiontimeout If the connection time-out is set, the connection times out.  * @return * @throws malformedurlexception * @throws ioexception * @throws jsonexception * @throws parseexception */private Boolean postsend (Final map<string, string> params,final string url, Final String encoding, Boolean setTimeout, Fina l int connectiontimeout) throws Malformedurlexception, IOException, jsonexception,parseexception {//Encapsulates a key-value pair for the request parameter. list<basicnamevaluepair> pairs = new arraylist<basicnamevaluepair> (); for (entry<string, String> Param:params.entrySet ()) {Pairs.add (New Basicnamevaluepair (Param.getkey (), Param.getvalue ()));} The entity that encapsulates the request parameter. urlencodedformentity entity = new urlencodedformentity (pairs,encoding);//use a POST request. HttpPost post = new HttpPost (URL);p ost.setentity (entity);//Use Defaulthttpclient to specify the request to get the response information. Defaulthttpclient client = new Defaulthttpclient ();//Set Connection timeout. if (setTimeout) {final Httpparams httpParameters = Client.getparams (); Httpconnectionparams.setconnectiontimeout (Httpparameters, connectiontimeout);} HttpResponse response = Client.execute (POST); BufferedReader reader = new BufferedReader (New InputStreamReader (Response.getentity (). GetContent (), encoding));// Use thread-safe stringbuffer.stringbuffer buffer = new StringBuffer (); String ln = null;while ((ln = reader.readline ()) = null) buffer.append (LN). Append (System.getproperty ("Line.separator") );//Line wrapping according to operating system platform.//Pass JSON to service processing. Final String rawjsonstr = buffer.tostring ();//test only://log.i ("Sysout", RAWJSONSTR); resultaqidays = Convertjsontoaqidaylist (RAWJSONSTR); return Response.getstatusline (). GetStatusCode () = = 200; Equals 200 indicates a successful send.}

Android-defaulthttpclient set timeout.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.