【Android】Http請求

來源:互聯網
上載者:User

比較簡單直接貼代碼了。

package jftt.test;</p><p>import java.io.IOException;<br />import java.util.List;</p><p>import org.apache.http.HttpResponse;<br />import org.apache.http.HttpStatus;<br />import org.apache.http.NameValuePair;<br />import org.apache.http.client.ClientProtocolException;<br />import org.apache.http.client.HttpClient;<br />import org.apache.http.client.entity.UrlEncodedFormEntity;<br />import org.apache.http.client.methods.HttpGet;<br />import org.apache.http.client.methods.HttpPost;<br />import org.apache.http.impl.client.DefaultHttpClient;<br />import org.apache.http.params.BasicHttpParams;<br />import org.apache.http.params.HttpConnectionParams;<br />import org.apache.http.params.HttpParams;</p><p>import android.util.Log;</p><p>public class HttpRequest {<br />/**<br /> *Post請求<br /> */<br />public void doPost(String url , List<NameValuePair> nameValuePairs){<br />//建立HttpClient對象<br />HttpClient httpclient = new DefaultHttpClient();<br />//建立POST串連<br />HttpPost httppost = new HttpPost(url);<br />try {<br />////使用PSOT方式,必須用NameValuePair數組傳遞參數<br />//List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();<br />//nameValuePairs.add(new BasicNameValuePair("id", "12345"));<br />//nameValuePairs.add(new BasicNameValuePair("stringdata","hps is Cool!"));<br />httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));<br />HttpResponse response = httpclient.execute(httppost);<br />} catch (ClientProtocolException e) {<br />e.printStackTrace();<br />} catch (IOException e) {<br />e.printStackTrace();<br />}<br />}</p><p>/**<br /> *Get請求<br /> */<br />public void doGet(String url){<br />HttpParams httpParams = new BasicHttpParams();<br /> HttpConnectionParams.setConnectionTimeout(httpParams,30000);<br /> HttpConnectionParams.setSoTimeout(httpParams, 30000); </p><p> HttpClient httpClient = new DefaultHttpClient(httpParams);<br />// GET<br /> HttpGet httpGet = new HttpGet(url);<br />try {<br />HttpResponse response = httpClient.execute(httpGet);<br />if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK){<br />Log.i("GET", "Bad Request!");<br />}<br />} catch (IOException e) {<br />e.printStackTrace();<br />}</p><p>}<br />}<br />

需要主要的是:

1.    使用POST方式時,傳遞參數必須使用NameValuePair數組

2.    使用GET方式時,通過URL傳遞參數,注意寫法

3.      通過setEntity方法來發送HTTP請求

4.      通過DefaultHttpClient 的 execute方法來擷取HttpResponse

5. 通過getEntity()從Response中擷取內容

 

String content = EntityUtils.toString(response.getEntity());

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.