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

歡迎訪問 生活随笔!

生活随笔

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

Android

Android如何使用NoHttp

發布時間:2025/4/14 Android 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android如何使用NoHttp 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
NoHttp 源碼及Demo托管在Github歡迎大家Star: https://github.com/yanzhenjie/NoHttp

NoHttp是專門做Android網絡請求與下載的框架。


NoHttp?是專門做Android網絡請求與下載的框架。

支持HTTP/HTTPS,自動維持Cookie,異步/同步請求,大文件/多文件上傳,文件下載;支持304緩存,302/303重定向,支持代理服務器。

NoHttp特性:

  • 支持HTTP/HTTPS,自動維持Cookie,異步/同步請求,大文件/多文件上傳,文件下載,上傳下載均有進度。

  • 支持304緩存,自定義緩存,302/303重定向,支持代理服務器訪問地址(如: Google)。

  • NoHttp是隊列,自動為請求排隊,可以取消指定請求, 可以取消隊列所有請求,亦可以停止隊列。

  • 支持請求String、Bitmap、Json、JavaBean,可自定義擴展請求類型。

  • Request對象包涵參數、文件、請求頭等;Response對象包涵響應內容,響應頭等信息,Cookie。

使用Gradle構建時添加依賴:

第一種, 使用jar包

NoHttp是一個純Java代碼的框架,沒有資源文件,因此可以使用jar包來開發,jar包可以在上面源碼的連接中下載到。

第二種, 使用Gradle添加依賴

?
1 2 3 4 //?引用最新版 compile?'com.yolanda.nohttp:nohttp:+' //?或則引用指定版本 compile?'com.yolanda.nohttp:nohttp:1.0.0'

第三種

使用源碼作為module,源碼可以在文章開頭和結尾的連接中下載到。


NoHttp 源碼及Demo托管在Github歡迎大家Star: https://github.com/yanzhenjie/NoHttp


一. 請求

1.請求String數據

?// 請求對象 Request<String> request = NoHttp.createStringRequest(url, requestMethod); //添加請求頭 request.addHeader("AppVersioin", "2.0"); // 添加請求參數 request.add("userName", "yolanda"); //上傳文件 request.add("file", new FileBinary(file));
2.請求Json數據

// JsonObject Request<JSONObject> request = NoHttp.createJsonObjectRequest(url, reqeustMethod); queue.add(what, request, responseListener); … // JsonArray Request<JSONArray> request = NoHttp.createJsonArrayRequest(url, reqeustMethod); queue.add(what, request, responseListener);
3. 請求Bitmap數據

Request<Bitmap>?request?=?NoHttp.createImageRequest(url,?requestMethod); ... 4. 取消請求

取消單個請求

Request<String>?request?=?NoHttp.createStringRequest(url); ... request.cancel();

從隊列中取消指定的請求

Request<String>?request?=?NoHttp.createStringRequest(url); request.setCancelSign(sign); … queue.cancelBySign(sign);

取消隊列中所有請求

queue.cancelAll();

停止隊列

RequestQueue?queue?=?NoHttp.newRequestQueue(); ... queue.stop();

5. 同步請求

//?在當前線程發起請求,在線程這么使用 Request<String>?request?=?NoHttp.createStringRequest(url); Response<String>?response?=?NoHttp.startRequestSync(request); if?(response.isSucceed())?{ ????//?請求成功 }?else?{ ????//?請求失敗 }

二. 緩存

1. Http標準協議的緩存,比如響應碼是304時

現在很多公司使用了RESTFUL風格來寫Http API,所以這個是必須有的。

Request<JSONObject>?request?=?NoHttp.createJsonObjectRequest(url); //?NoHttp本身是RESTFUL風格的標準Http協議,所以這里不用設置或者設置為DEFAULT request.setCacheMode(CacheMode.DEFAULT); ... 2. 當請求服務器失敗的時候,讀取緩存

Request<JSONObject>?request?=?NoHttp.createJsonObjectRequest(url); //?非標準Http協議,改變緩存模式為REQUEST_FAILED_READ_CACHE request.setCacheMode(CacheMode.REQUEST_FAILED_READ_CACHE); ... 3. 如果發現有緩存直接成功,沒有緩存才請求服務器

我們知道ImageLoader的核心除了內存優化外,剩下一個就是發現把內地有圖片則直接使用,沒有則請求服務器,所以NoHttp這一點非常使用做一個ImageLoader。

Request<JSONObject>?request?=?NoHttp.createJsonObjectRequest(url); //?非標準Http協議,改變緩存模式為IF_NONE_CACHE_REQUEST request.setCacheMode(CacheMode.IF_NONE_CACHE_REQUEST); ... 請求圖片,緩存圖片。

//?如果沒有緩存才去請求服務器,否則使用緩存,緩存圖片演示 Request<Bitmap>?request?=?NoHttp.createImageRequest(imageUrl); request.setCacheMode(CacheMode.IF_NONE_CACHE_REQUEST); ...

4. 僅僅讀取緩存

Request<JSONObject>?request?=?NoHttp.createJsonObjectRequest(url); //?非標準Http協議,改變緩存模式為ONLY_READ_CACHE request.setCacheMode(CacheMode.ONLY_READ_CACHE); ... 注意:緩存不管是String、Json、圖片還是任何請求都可以被NoHttp緩存

二、響應

OnResponseListener<String> responseListener = new OnResponseListener<String>() {@Overridepublic void onStart(int what) {// 請求開始時,可以顯示一個Dialog}@Overridepublic void onFinish(int what) {// 請求接受時,關閉Dialog}@Overridepublic void onSucceed(int what, Response<String> response) {// 接受請求結果String result = response.get();// Bitmap imageHead = response.get(); // 如果是bitmap類型,都是同樣的用法}@Overridepublic void onFailed(int what, String url, Object tag, Exception exception, ...) {// 請求失敗或者發生異常// 這里根據exception處理不同的錯誤,比如超時、網絡不好等} };

三. 自定義請求類型: FastJsonRequest

1.定義請求對象

public class FastJsonRequest extends RestRequestor<JSONObject> {public FastJsonRequest(String url) {super(url); }public FastJsonRequest(String url, RequestMethod requestMethod) {super(url, requestMethod); }@Override public JSONObject parseResponse(String url, Headers headers, byte[] responseBody) {String result = StringRequest.parseResponseString(url, headers, responseBody);JSONObject jsonObject = null;if (!TextUtils.isEmpty(result)) {jsonObject = JSON.parseObject(result);} else {// 這里默認的錯誤可以定義為你們自己的數據格式jsonObject = JSON.toJSON("{}");}return jsonObject; }@Override public String getAccept() {// 告訴服務器你接受什么類型的數據, 會添加到請求頭的Accept中return "application/json;q=1"; }}
2.使用自定義請求-和NoHttp默認請求沒有區別

Request<JSONObject>?mRequest?=?new?FastJsonRequest(url,?requestMethod); queue.add(what,?mRequest,?responseListener);

五. 下載文件

發起下載請求

//下載文件 downloadRequest = NoHttp.createDownloadRequest(url, fielDir, fileName, true, false); // what 區分下載 // downloadRequest 下載請求對象 // downloadListener 下載監聽 CallServer.getDownloadInstance().add(0, downloadRequest, downloadListener);
暫停或者停止下載
1 downloadRequest.cancel();
監聽下載過程

private DownloadListener downloadListener = new DownloadListener() {@Overridepublic void onStart(int what, boolean resume, long preLenght, Headers header, long count) {}@Overridepublic void onProgress(int what, int progress, long downCount) {// 更新下載進度}@Overridepublic void onFinish(int what, String filePath) {}@Overridepublic void onDownloadError(int what, StatusCode code, CharSequence message) {}@Overridepublic void onCancel(int what) {} };
原文:http://www.oschina.net/p/nohttp

轉載于:https://www.cnblogs.com/sharecenter/p/5620976.html

總結

以上是生活随笔為你收集整理的Android如何使用NoHttp的全部內容,希望文章能夠幫你解決所遇到的問題。

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