生活随笔
收集整理的這篇文章主要介紹了
【Android进阶学习】Http编程之HttpClient
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?在Android開發(fā)中,Android SDK附帶了Apache的HttpClient,它是一個(gè)完善的客戶端。它提供了對(duì)HTTP協(xié)議的全面支持,可以使用HttpClient的對(duì)象來執(zhí)行HTTP GET和HTTP POST調(diào)用。
HTTP工作原理:
1.客戶端(一般是指瀏覽器,這里是指自己寫的程序)與服務(wù)器建立連接
2.建立連接后,客戶端向服務(wù)器發(fā)送請(qǐng)求
3.服務(wù)器接收到請(qǐng)求后,向客戶端發(fā)送響應(yīng)信息
4.客戶端與服務(wù)器斷開連接
HttpClient的一般使用步驟:
1.使用DefaultHttpClient類實(shí)例化HttpClient對(duì)象
2.創(chuàng)建HttpGet或HttpPost對(duì)象,將要請(qǐng)求的URL通過構(gòu)造方法傳入HttpGet或HttpPost對(duì)象。
3.調(diào)用execute方法發(fā)送HTTP GET或HTTP POST請(qǐng)求,并返回HttpResponse對(duì)象。
4.通過HttpResponse接口的getEntity方法返回響應(yīng)信息,并進(jìn)行相應(yīng)的處理。
最后記得要在AndroidManifest.xml文件添加網(wǎng)絡(luò)權(quán)限
<uses-permission android:name="android.permission.INTERNET" />
?下面是具體的例子:
1.使用HttpClient來執(zhí)行GET調(diào)用
在LogCat窗口就能看到輸出的信息
package?com.lingdududu.http; ?? import?java.io.InputStream; ? ? import?org.apache.http.HttpResponse; ? import?org.apache.http.HttpStatus; ? import?org.apache.http.client.HttpClient; ? import?org.apache.http.client.methods.HttpGet; ? import?org.apache.http.impl.client.DefaultHttpClient; ? ? import?android.app.Activity; ? import?android.os.Bundle; ? import?android.util.Log; ? ? public?class?HttpGetActivity?extends?Activity?{ ? ????String?uri?=?"http://developer.android.com/"; ? ????final?String?TAG_STRING?=?"TAG"; ? ????@Override? ????public?void?onCreate(Bundle?savedInstanceState)?{ ? ????????super.onCreate(savedInstanceState); ? ????????setContentView(R.layout.main); ????????? ? ????????try?{ ? ????????????? ????????????HttpClient?getClient?=?new?DefaultHttpClient(); ? ????????????? ????????????HttpGet?request?=?new?HttpGet(uri); ? ????????????? ????????????HttpResponse?response?=?getClient.execute(request); ? ????????????? ????????????if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){ ? ????????????????Log.i(TAG_STRING,?"請(qǐng)求服務(wù)器端成功"); ? ????????????????? ????????????????InputStream??inStrem?=?response.getEntity().getContent(); ? ????????????????int?result?=?inStrem.read(); ? ????????????????while?(result?!=?-1){ ? ????????????????????System.out.print((char)result); ? ????????????????????result?=?inStrem.read(); ?????????????????} ? ????????????????? ????????????????inStrem.close();???? ? ????????????}else?{ ? ????????????????Log.i(TAG_STRING,?"請(qǐng)求服務(wù)器端失敗"); ? ????????????}??????????? ? ????????}?catch?(Exception?e)?{ ? ????????????? ????????????e.printStackTrace(); ?????????} ?????} ?}?使用HTTP GET調(diào)用有一個(gè)缺點(diǎn)就是,請(qǐng)求的參數(shù)作為URL一部分來傳遞,以這種方式傳遞的時(shí)候,URL的長(zhǎng)度應(yīng)該在2048個(gè)字符之內(nèi)。如果超出這個(gè)這范圍,就要使用到HTTP POST調(diào)用。
2.使用HttpClient來執(zhí)行POST調(diào)用
?使用POST調(diào)用進(jìn)行參數(shù)傳遞時(shí),需要使用NameValuePair來保存要傳遞的參數(shù)。NameValuePair封裝了一個(gè)鍵/值組合。另外,還需要設(shè)置所使用的字符集。
package?com.androidbook.services.httppost; ?? import?java.io.BufferedReader; ? import?java.io.IOException; ? import?java.io.InputStreamReader; ? import?java.util.ArrayList; ? import?java.util.List; ? ? import?org.apache.http.HttpResponse; ? import?org.apache.http.NameValuePair; ? import?org.apache.http.client.HttpClient; ? import?org.apache.http.client.entity.UrlEncodedFormEntity; ? import?org.apache.http.client.methods.HttpPost; ? import?org.apache.http.impl.client.DefaultHttpClient; ? import?org.apache.http.message.BasicNameValuePair; ? ? import?android.app.Activity; ? import?android.os.Bundle; ? ? public?class?HttpPostActivity?extends?Activity?{ ? ????String?uri?=?"http://developer.android.com/"; ? ????@Override? ????public?void?onCreate(Bundle?savedInstanceState)?{ ? ????????super.onCreate(savedInstanceState); ? ????????setContentView(R.layout.main); ?? ????????BufferedReader?in?=?null; ? ????????try?{ ? ????????????HttpClient?client?=?new?DefaultHttpClient(); ? ????????????HttpPost?request?=?new?HttpPost("http://code.google.com/android/"); ? ????????????? ????????????List<NameValuePair>?postParameters?=?new?ArrayList<NameValuePair>(); ? ????????????? ????????????postParameters.add(new?BasicNameValuePair("id",?"12345")); ? ????????????postParameters.add(new?BasicNameValuePair("username",?"dave")); ? ????????????? ????????????UrlEncodedFormEntity?formEntity?=?new?UrlEncodedFormEntity( ? ????????????????????postParameters); ?? ????????????? ????????????request.setEntity(formEntity); ?????????????HttpResponse?response?=?client.execute(request); ? ????????????in?=?new?BufferedReader( ? ????????????????????new?InputStreamReader( ? ????????????????????????????response.getEntity().getContent())); ?? ????????????StringBuffer?string?=?new?StringBuffer(""); ? ????????????String?lineStr?=?""; ? ????????????while?((lineStr?=?in.readLine())?!=?null)?{ ? ????????????????string.append(lineStr?+?"\n"); ? ????????????} ?????????????in.close(); ??????????????String?resultStr?=?string.toString(); ?????????????System.out.println(resultStr); ? ????????}?catch(Exception?e)?{ ? ????????????? ????????}?finally?{ ? ????????????if?(in?!=?null)?{ ? ????????????????try?{ ? ????????????????????in.close(); ? ????????????????}?catch?(IOException?e)?{ ? ????????????????????e.printStackTrace(); ?????????????????} ?????????????} ?????????} ?????} ?}??
本文轉(zhuǎn)自 lingdududu 51CTO博客,原文鏈接:?
http://blog.51cto.com/liangruijun/803097
總結(jié)
以上是生活随笔為你收集整理的【Android进阶学习】Http编程之HttpClient的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。