Android uses Apache httpclient to send post requests

Source: Internet
Author: User

Package Com.liuc;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import java.io.InputStream;import Java.io.unsupportedencodingexception;import Java.util.arraylist;import Java.util.list;import Java.util.Map;import Org.apache.http.httpresponse;import Org.apache.http.namevaluepair;import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.httpclient;import Org.apache.http.client.config.requestconfig;import Org.apache.http.client.entity.urlencodedformentity;import Org.apache.http.client.methods.httppost;import Org.apache.http.impl.client.httpclients;import Org.apache.http.message.basicnamevaluepair;public class Httppostutil {public static String Sendhttppost (map< String, string> map,string encode,string path) {list<namevaluepair> list=new arraylist<namevaluepair> ( if (Map!=null&&!map.isempty ()) {for (map.entry<string, string> entry:map.entrySet ()) {List.add (new Basicnamevaluepair (Entry.getkey (), Entry.getvalue ()));}} TryThe {///implementation encapsulates the parameters into the form, i.e. request weight urlencodedformentity entity=new urlencodedformentity (List,encode);//submit data using Post HttpPost Httppost=new httppost (path); httppost.setentity (entity); Requestconfig requestconfig = Requestconfig.custom (). SetSocketTimeout (.). Setconnecttimeout (. build);// Setup request and transfer timeout after 4.3 httppost.setconfig (requestconfig);//Perform POST request//3.x this is//httpclient httpclient=new Defaulthttpclient ();//4.x is such a httpClient httpClient = Httpclients.createdefault (); HttpResponse Httpresponse=httpclient.execute (HttpPost); if (Httpresponse.getstatusline (). GetStatusCode () ==200) { Return Getstringforinputstream (Httpresponse.getentity (). GetContent (), encode);}} catch (Unsupportedencodingexception e) {e.printstacktrace ();} catch (Clientprotocolexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} return path;} private static String Getstringforinputstream (InputStream inputstream,string encode) {Bytearrayoutputstream stream= New Bytearrayoutputstream (); byte[] Data=new byte[1024];int len=0; String result= "", try {if (InputStream! = null) {while (len = inputstream.read (data))! =-1) {stream.write (data, 0, Len);} Result=new String (Stream.tobytearray (), encode);}} catch (Exception e) {}return result;}}

The

differs for each version of the notation. can refer to: http://www.open-open.com/lib/view/open1383751765321.html

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.