okhttp-应用

1. Request

String request

// String request object
Request<String> request = NoHttp.createStringRequest(url, requestMethod);

Json request

// JsonObject
Request<JSONObject> request = NoHttp.createJsonObjectRequest(url, reqeustMethod);
...
// JsonArray
Request<JSONArray> request = NoHttp.createJsonArrayRequest(url, reqeustMethod);

Bitmap request

Request<Bitmap> request = NoHttp.createImageRequest(url, requestMethod);

Add params

Request<JSONObject> request = ...
request.add("name", "yoldada");// String type
request.add("age", 18);// int type
request.add("sex", '0')// char type
request.add("time", 16346468473154); // long type
...

Add to queue

RequestQueue requestQueue = NoHttp.newRequestQueue();
// Or a concurrent value, allowing three concurrent requests at the same time
// RequestQueue requestQueue = NoHttp.newRequestQueue(3);

// start request
requestQueue.add(what, request, responseListener);

  Added to the queue above there is a credit, the credit will be responseLisetener response callback to developers,so we can use a responseLisetener accept multiple requests the response,with what to distinguish results. Instead, like some framework, each request to the new a callback.

Synchronous request

  In request, the current thread by the thread so used.

Request<String> request = ...
Response<String> response = NoHttp.startRequestSync(request);
if (response.isSucceed()) {
    // succeed
} else {
    // failed
}

2. File upload

  Support for multiple File upload, multiple key multiple files, a key multiple files (List < File >). Support File, InputStream, ByteArray containing, Bitmap, realize NoHttp Binary interface, anything can theoretically.

A single file

Request<String> request = ...
request.add("file", new FileBinary(file));

Upload multiple files, multiple Key file form

  Here you can add all kinds of files,File、Bitmap、InputStream、ByteArray:

Request<String> request = ...
request.add("file1", new FileBinary(File));
request.add("file2", new FileBinary(File));
request.add("file3", new InputStreamBinary(InputStream));
request.add("file4", new ByteArrayBinary(byte[]));
request.add("file5", new BitmapBinary(Bitmap));

Upload multiple files, a Key form of multiple files

  With the same key to add, the same key will not be covered.

Request<String> request = ...
fileList.add("image", new FileBinary(File));
fileList.add("image", new InputStreamBinary(InputStream));
fileList.add("image", new ByteArrayBinary(byte[]));
fileList.add("image", new BitmapBinary(Bitmap));

 Or: 

Request<String> request = ...

List<Binary> fileList = ...
fileList.add(new FileBinary(File));
fileList.add(new InputStreamBinary(InputStream));
fileList.add(new ByteArrayBinary(byte[]));
fileList.add(new BitmapBinary(Bitmap));
request.add("file_list", fileList);

3. Download file

  Because the downloaded file code is more, post key part, specific please refer to the sample.

Request a download

// download file
downloadRequest = NoHttp.createDownloadRequest...
// what To distinguish the download.
// downloadRequest To download the request object.
// downloadListener Listeningof download.
downloadQueue.add(0, downloadRequest, downloadListener);

Pause or stop the download

downloadRequest.cancel();

Listening to the download process

private DownloadListener downloadListener = new DownloadListener() {
    @Override
    public void onStart(int what, boolean resume, long preLenght, Headers header, long count) {
        // download start.
    }

    @Override
    public void onProgress(int what, int progress, long downCount) {
        // update download progress.
    }

    @Override
    public void onFinish(int what, String filePath) {
        // download finish.
    }

    @Override
    public void onDownloadError(int what, StatusCode code, CharSequence message) {
        // download have a error.
    }

    @Override
    public void onCancel(int what) {
        // Download has been cancelled or suspended.
    }
};

;

4. Cache mode

1. Cache Http standard protocols, such as when the response code is 304(redirect)

  Is realized RFC2616 NoHttp itself, so there need not set or set to DEFAULT.

Request<JSONObject> request = NoHttp.createJsonObjectRequest(url);
request.setCacheMode(CacheMode.DEFAULT);

2. When the request network failure, and then read the cache

  Request to the server success is returned data server, if the request to the server fails, read the cache data to return.

Request<JSONObject> request = NoHttp.createJsonObjectRequest(url);
request.setCacheMode(CacheMode.REQUEST_NETWORK_FAILED_READ_CACHE);

3. To read the first cache, there is no cache request to the server again

  We know that the core of the ImageLoader besides memory optimization, one is found that the mainland has left image is used directly, without the request to the server, so do a ImageLoader NoHttp this very use.   If there is no cache request to the server, or use the cache:

Request<JSONObject> request = NoHttp.createJsonObjectRequest(url);
// Change the caching pattern to IF_NONE_CACHE_REQUEST_NETWORK.
request.setCacheMode(CacheMode.IF_NONE_CACHE_REQUEST_NETWORK);

  Request pictures, cache the image:

Request<Bitmap> request = NoHttp.createImageRequest(imageUrl);
request.setCacheMode(CacheMode.IF_NONE_CACHE_REQUEST_NETWORK);

4. Only request network

  Here can't read the cache, can't use Http304:

Request<Bitmap> request = NoHttp.createImageRequest(imageUrl);
request.setCacheMode(CacheMode.ONLY_REQUEST_NETWORK);
...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值