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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發布時間:2024/4/14 Android 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Android进阶学习】Http编程之HttpClient 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?在Android開發中,Android SDK附帶了Apache的HttpClient,它是一個完善的客戶端。它提供了對HTTP協議的全面支持,可以使用HttpClient的對象來執行HTTP GET和HTTP POST調用。

HTTP工作原理:

1.客戶端(一般是指瀏覽器,這里是指自己寫的程序)與服務器建立連接

2.建立連接后,客戶端向服務器發送請求

3.服務器接收到請求后,向客戶端發送響應信息

4.客戶端與服務器斷開連接

HttpClient的一般使用步驟:

1.使用DefaultHttpClient類實例化HttpClient對象

2.創建HttpGet或HttpPost對象,將要請求的URL通過構造方法傳入HttpGet或HttpPost對象。

3.調用execute方法發送HTTP GET或HTTP POST請求,并返回HttpResponse對象。

4.通過HttpResponse接口的getEntity方法返回響應信息,并進行相應的處理。

最后記得要在AndroidManifest.xml文件添加網絡權限

<uses-permission android:name="android.permission.INTERNET" />

?下面是具體的例子:

1.使用HttpClient來執行GET調用

在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對象 ?
  • ????????????HttpClient?getClient?=?new?DefaultHttpClient(); ?
  • ????????????//得到HttpGet對象 ?
  • ????????????HttpGet?request?=?new?HttpGet(uri); ?
  • ????????????//客戶端使用GET方式執行請教,獲得服務器端的回應response ?
  • ????????????HttpResponse?response?=?getClient.execute(request); ?
  • ????????????//判斷請求是否成功?? ?
  • ????????????if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){ ?
  • ????????????????Log.i(TAG_STRING,?"請求服務器端成功"); ?
  • ????????????????//獲得輸入流 ?
  • ????????????????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,?"請求服務器端失敗"); ?
  • ????????????}??????????? ?
  • ????????}?catch?(Exception?e)?{ ?
  • ????????????//?TODO?Auto-generated?catch?block ?
  • ????????????e.printStackTrace(); ?
  • ????????} ?
  • ????} ?
  • }?
  • 使用HTTP GET調用有一個缺點就是,請求的參數作為URL一部分來傳遞,以這種方式傳遞的時候,URL的長度應該在2048個字符之內。如果超出這個這范圍,就要使用到HTTP POST調用。

    2.使用HttpClient來執行POST調用

    ?使用POST調用進行參數傳遞時,需要使用NameValuePair來保存要傳遞的參數。NameValuePair封裝了一個鍵/值組合。另外,還需要設置所使用的字符集。

  • 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參數 ?
  • ????????????List<NameValuePair>?postParameters?=?new?ArrayList<NameValuePair>(); ?
  • ????????????//添加要傳遞的參數?? ?
  • ????????????postParameters.add(new?BasicNameValuePair("id",?"12345")); ?
  • ????????????postParameters.add(new?BasicNameValuePair("username",?"dave")); ?
  • ????????????//實例化UrlEncodedFormEntity對象 ?
  • ????????????UrlEncodedFormEntity?formEntity?=?new?UrlEncodedFormEntity( ?
  • ????????????????????postParameters); ?
  • ?
  • ????????????//使用HttpPost對象來設置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(); ?
  • ????????????????} ?
  • ????????????} ?
  • ????????} ?
  • ????} ?
  • }?
  • ?


    本文轉自 lingdududu 51CTO博客,原文鏈接:?

    http://blog.51cto.com/liangruijun/803097

    總結

    以上是生活随笔為你收集整理的【Android进阶学习】Http编程之HttpClient的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。