android使用apache httpclient發送post請求,androidhttpclient

來源:互聯網
上載者:User

android使用apache httpclient發送post請求,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<NameValuePair>();if (map!=null&&!map.isEmpty()) {for(Map.Entry<String, String> entry:map.entrySet()){list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));}}try {//實現將參數封裝到表單中,也就是請求體重UrlEncodedFormEntity entity=new UrlEncodedFormEntity(list,encode);//使用post方式提交資料HttpPost httpPost=new HttpPost(path);httpPost.setEntity(entity);RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();//4.3之後的佈建要求和傳輸逾時時間httpPost.setConfig(requestConfig);//執行post請求//3.X是這樣的//HttpClient httpClient=new DefaultHttpClient();//4.x是這樣的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;}}

對於各個版本寫法的區別。可以參考:http://www.open-open.com/lib/view/open1383751765321.html


java 使用HttpClient發送post的問題

Post請求是通過這種索引值對參數的形式,添全URL那是get方式
 
Android 怎用HttpClient 以Post方式提交資料並添加http頭資訊

//添加http頭資訊httppost.addHeader(Authorization, your token); //認證tokenhttppost.addHeader(Content-Type, application/json);httppost.addHeader(User-Agent, imgfornote);//http post的json資料格式: {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.execute(httppost);//檢驗狀態代碼,如果成功接收資料int code = response.getStatusLine().getStatusCode();if (code == 200) {String rev = EntityUtils.toString(response.getEntity());//返回json格式: {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) {}}主要用到的類有:org.apache.http.client.HttpClient 、org.apache.http.client.methods.HttpPost 和org.json.JSONObject
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.