NoHttp, feeling spill(NoHttp,情怀四溢).
The author's url: http://www.yanzhenjie.com
The author's blog: http://blog.csdn.net/yanzhenjie1003
NoHttp home page: http://www.nohttp.net
NoHttp document: http://doc.nohttp.net
NoHttp source code: https://github.com/yanzhenjie/NoHttp
Document also continues to improve, there is a problem can send issues, or send an email to me directly: [email protected].
- Eclipse use NoHttp Jar, if need to rely on the source code, download.
download jar [include source code,274k]
download jar [not include source code,147k]
- AndroidStudio using Gradle build add dependent (recommended)
compile 'com.yolanda.nohttp:nohttp:1.0.5'
Downloa source code of NoHttp, Demo
Download Demo APK
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
NoHttp implements Http1.1 (RFC2616), a standard Http framework.
- Request and download queue, average distribution of each thread resources, support for multiple requests concurrently.
- Support the GET, POST, PUT, PATCH, HEAD, DELETE, OPTIONS, such as TRACE request protocol.
- Support for POST, PUT, PATCH, DELETE the file upload (Html form principle).
- File download, upload, download, upload and download the progress of the callback, error correction.
- Provides five kinds of data caching policy for developers to choose to use (see below) in detail.
- Support to cancel a request, specify multiple request cancel, cancel all requests.
- Support custom Request, using NoHttp generics can be parsed into any data you want to format (String, Json, JavaBean, etc.).
- Automatic maintenance of support Session, cookies, restart the App, turn off the boot after also continue to maintain.
- Support Https, self-signed website Https access, support two-way authentication.
NoHttp provides a debug mode, after open the can see clear request process, such as how to transfer data, basic need not caught. Can see that the request headers, request data, the process of response headers, cookies, etc. You don't have to worry about too many Log will dazzle you, clean and tidy.
- Support the request String, Json, FastJson, Gson, Bitmap, javabeans and XML extensions.
- An asynchronous request, get the results directly to update the UI, support synchronous request.
All download progress callback, error correction and other friendly interface.
- Large file upload, OOM will not occur.
- File upload more, multiple key file, a key multiple files(
List<File>
).- Support File, InputStream, ByteArray containing, Bitmap, realize NoHttp Binary interface, anything can theoretically.
- Support to cancel the upload.
- File to download, support for multiple files to download at the same time, and a progress callback, error correction and so on.
- Support for a moratorium on continue to download, support to cancel the download, support breakpoint continuingly.
- To make use of much NoHttp file download can do a download manager.
- Only request network.
- Only read cache.
- The standard Http protocol cache (such as in the case of the response code is 304), need server support, if the server does not support the same way as a common request.
- First request network, after failing to return to the cache.
- First read the cache, the cache request network again.
All cancelled all support requests are being executed.
- Support cancel a request。
- Support to cancel with sign to specify several requests。
- Support cancel all request。
- Maintain support Session, cookies, temporary cookies.
- Support the App after the restart, shutdown boot continue to persistent.
- Provides the interface that allows developers to monitor the change of the cookies, can also change the value of a Cookie.
- For Http301, 302, 303, 307 redirect support.
- Support multiple redirection nesting.
- Support disable redirection, NoHttp provides redirection interface operation.
- The standard Java Api, the ProXy: the designated agent of IP and Port.
- Such as agent to your computer for caught when debugging, such as use proxy to access Google.
// String request object
Request<String> request = NoHttp.createStringRequest(url, requestMethod);
// JsonObject
Request<JSONObject> request = NoHttp.createJsonObjectRequest(url, reqeustMethod);
...
// JsonArray
Request<JSONArray> request = NoHttp.createJsonArrayRequest(url, reqeustMethod);
Request<Bitmap> request = NoHttp.createImageRequest(url, requestMethod);
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
...
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.
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
}
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.
Request<String> request = ...
request.add("file", new FileBinary(file));
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));
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);
Because the downloaded file code is more, post key part, specific please refer to the sample.
// download file
downloadRequest = NoHttp.createDownloadRequest...
// what To distinguish the download.
// downloadRequest To download the request object.
// downloadListener Listeningof download.
downloadQueue.add(0, downloadRequest, downloadListener);
downloadRequest.cancel();
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.
}
};
;
Is realized RFC2616 NoHttp itself, so there need not set or set to DEFAULT.
Request<JSONObject> request = NoHttp.createJsonObjectRequest(url);
request.setCacheMode(CacheMode.DEFAULT);
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);
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);
Here can't read the cache, can't use Http304:
Request<Bitmap> request = NoHttp.createImageRequest(imageUrl);
request.setCacheMode(CacheMode.ONLY_REQUEST_NETWORK);
...
Just read the cache, not request network and other operation:
Request<Bitmap> request = NoHttp.createImageRequest(imageUrl);
request.setCacheMode(CacheMode.ONLY_READ_CACHE);
User method cancel()
of request.
request.cancel();
To the request set a sign, to cancel the call queue cancelBySign can cancel all specify the sign of the request.
request.setCancelSign(sign);
...
queue.cancelBySign(sign);
queue.cancelAll();
After add the request to the queue in the queue to stop, the request will not be executed.
RequestQueue queue = NoHttp.newRequestQueue();
...
queue.stop();
#6. The custom request type: FastJsonRequest
public class FastJsonRequest extends RestRequestor<JSONObject> {
public FastJsonRequest(String url) {
this(url, RequestMethod.GET);
}
public FastJsonRequest(String url, RequestMethod requestMethod) {
super(url, requestMethod);
setHeader("application/json");
}
@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;
}
}
Request<JSONObject> mRequest = new FastJsonRequest(url, requestMethod);
queue.add(what, mRequest, responseListener);
NoHttp All classes can be confusing.
NoHttp1.0.0 Using the leve23 API, so have to use leve23 packaging. NoHttp1.0.1 and higher using the reflection calls the high-level or low-level apis, so as long as it is more than leve9 SDK can be compiled.
-dontwarn com.yolanda.nohttp.**
-keep class com.yolanda.nohttp.**{*;}
#License
Copyright 2016 Yan Zhenjie
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.