Android uses apache httpclient to send a post request, androidhttpclient
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 <NameVal UePair> (); if (map! = Null &&! Map. isEmpty () {for (Map. entry <String, String> entry: map. entrySet () {list. add (new BasicNameValuePair (entry. getKey (), entry. getValue () ;}}try {// encapsulate parameters in the form, that is, the request weight UrlEncodedFormEntity entity = new UrlEncodedFormEntity (list, encode ); // use the post method to submit data HttpPost httpPost = new HttpPost (path); httpPost. setEntity (entity); RequestConfig requestConfig = RequestConfig. custom (). setSocketTimeout (2000 ). setConnectTimeout (2000 ). build (); // set the request and transmission timeout After 4.3 httpPost. setConfig (requestConfig); // execute the post request // 3. X is like this // HttpClient httpClient = new DefaultHttpClient (); // 4. x is the HttpClient httpClient = HttpClients. createDefault (); HttpResponse httpResponse=httpClient.exe cute (httpPost); if (httpResponse. getStatusLine (). getStatusCode () = 200) {return getStringForInputStream (httpResponse. getEntity (). getContent (), encode) ;}} catch (Unsuppo RtedEncodingException 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 ;}}
Differences between different versions. See http://www.open-open.com/lib/view/open1383751765321.html
Question about using HttpClient to send post in java
THE Post request is in the form of a key-Value Pair parameter. Adding a full URL is the get method.
How does Android use HttpClient to submit data in Post mode and add http header information?
// Add http header information httppost. addHeader (Authorization, your token); // authenticates tokenhttppost. addHeader (Content-Type, application/json); httppost. addHeader (User-Agent, imgfornote); // json data format of http post: {name: your name, parentId: id_of_parent} JSONObject obj = new JSONObject (); obj. put (name, your name); obj. put (parentId, your parentid); httppost. setEntity (new StringEntity (obj. toString (); HttpResponse response; response = Httpclient.exe cute (httppost); // check the status code. If the data is received successfully, int code = response. getStatusLine (). getStatusCode (); if (code = 200) {String rev = EntityUtils. toString (response. getEntity (); // return the json format: {id: 27JpL ~ J4vsL0LX00E00005, version: abc} obj = new JSONObject (rev); String id = obj. getString (id); String version = obj. getString (version) ;}} catch (ClientProtocolException e) {}catch (Exception e) {}} mainly uses the following classes: org. apache. http. client. httpClient, org. apache. http. client. methods. httpPost and org. json. JSONObject