日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

【Android进阶学习】Http编程之HttpClient

發(fā)布時(shí)間:2024/4/14 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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對(duì)象 ?
  • ????????????HttpClient?getClient?=?new?DefaultHttpClient(); ?
  • ????????????//得到HttpGet對(duì)象 ?
  • ????????????HttpGet?request?=?new?HttpGet(uri); ?
  • ????????????//客戶端使用GET方式執(zhí)行請(qǐng)教,獲得服務(wù)器端的回應(yīng)response ?
  • ????????????HttpResponse?response?=?getClient.execute(request); ?
  • ????????????//判斷請(qǐng)求是否成功?? ?
  • ????????????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(); ?
  • ????????????????} ?
  • ????????????????//關(guān)閉輸入流 ?
  • ????????????????inStrem.close();???? ?
  • ????????????}else?{ ?
  • ????????????????Log.i(TAG_STRING,?"請(qǐng)求服務(wù)器端失敗"); ?
  • ????????????}??????????? ?
  • ????????}?catch?(Exception?e)?{ ?
  • ????????????//?TODO?Auto-generated?catch?block ?
  • ????????????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/"); ?
  • ????????????//使用NameValuePair來保存要傳遞的Post參數(shù) ?
  • ????????????List<NameValuePair>?postParameters?=?new?ArrayList<NameValuePair>(); ?
  • ????????????//添加要傳遞的參數(shù)?? ?
  • ????????????postParameters.add(new?BasicNameValuePair("id",?"12345")); ?
  • ????????????postParameters.add(new?BasicNameValuePair("username",?"dave")); ?
  • ????????????//實(shí)例化UrlEncodedFormEntity對(duì)象 ?
  • ????????????UrlEncodedFormEntity?formEntity?=?new?UrlEncodedFormEntity( ?
  • ????????????????????postParameters); ?
  • ?
  • ????????????//使用HttpPost對(duì)象來設(shè)置UrlEncodedFormEntity的Entity ?
  • ????????????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)?{ ?
  • ????????????//?Do?something?about?exceptions ?
  • ????????}?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ò),歡迎將生活随笔推薦給好友。