Comparison of several mainstream network frameworks in Android

      introduction

  At present, there are also many network request frameworks. This article compares the well-packaged Volley, Okhttp, Retrofit, and AsyncHttpClient to know which one is the most suitable.

1. Volley

1.Volley main features
    1. 扩展性强 
         基于接口设计。
    2. 一定程度上符合http规范。
         返回包括ResponseCode 的处理,请求头的处理,缓存机制的支持。
    3. 重试以及优先级的定义。
    4. 2.3以上基于HttpUrlconnection。 
          2.3以下是Httpclient(没有这号机子了吧)
    5. 提供简单的图片加载工具。

2. Use of Volley

Basic usage: http://www.kwstu.com/ArticleView/kwstu_20144118313429
If you return Object directly, use the combination of Gson/FastJson and Volley: http://www.cnblogs.com/freexiaoyu/p/3955137.html

3.Volley problem collection:

Volley's request is called back to the main thread by default. If there is a need to load into sqlite, etc., the operation solution that still needs to be performed in the sub-thread: https://www.zhihu.com/question/36672622/answer/76003423

2.Retrofit

Based on annotations, Retrofit provides JSON to POJO (Plain Ordinary Java Object, simple Java object), POJO to JSON, network request (POST, GET, PUT, DELETE, etc.) encapsulation.

3. The reputation of Okhttp is quite good.

The thread that this onResponse executes is not the UI thread.

OKHttp source code location https://github.com/square/okhttp
Source code analysis of bubble network: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0326/2643.html

Interceptor Cloud response interceptor, set cache policy
1) Application interceptors (Application Interceptors)
are mainly used to view request information and return information, such as link address, header information, parameter information, etc.
2) Network interceptors (Network Interceptors)
can be added and deleted Or replace the request header information, you can also change the entity carried by the request.

How to use the cache> http://blog.csdn.net/copy_yuan/article/details/51524907

 noCache :不使用缓存,全部走网络
 noStore : 不使用缓存,也不存储缓存
 onlyIfCached : 只使用缓存
 maxAge :设置最大失效时间,失效则不使用
 maxStale :设置最大失效时间,失效则不使用
 minFresh :设置最小有效时间,失效则不使用
 FORCE_NETWORK : 强制走网络
 FORCE_CACHE :强制走缓存

General get, post, file upload based on http, file download, load image, support request callback, directly return object, object collection, support session retention.
Automatically recovers from many common connection problems. If your server is configured with multiple IP addresses, when the first IP connection fails, OkHttp will automatically try the next IP. OkHttp also handles proxy server issues and SSL handshake failures.

3. AsyncHttpClient, easier to use

 1. Introduction
Network requests in Android generally use Apache HTTP Client or HttpURLConnect, but using these two libraries directly requires writing a lot of code to complete network post and get requests, and using the android-async-http library can greatly simplify Operation, it is based on Apache's HttpClient, all requests are independent from the UI main thread, the request results are processed through the callback method, and the android Handler message mechanism is used to transmit information.

2. Features
(1) Use asynchronous HTTP requests and process callback results through anonymous inner classes
(2) HTTP requests are independent from the main UI thread
(3) Use thread pools to process concurrent requests
(4) Use RequestParams class to create GET/ POST parameters
(5) Support Multipart file upload without third-party package
(6) Only 25kb in size
(7) Automatically handle disconnection requests for various mobile phones
(8) Ultra-fast automatic gzip response decoding Support
(9) Use the BinaryHttpResponseHandler class to download binary files (such as pictures)
(10) Use the JsonHttpResponseHandler class to automatically parse the response result into json format
(11) Persistent cookie storage, you can save the cookie to the SharedPreferences of your application

3. How to use
(1) Go to the official website http://loopj.com/android-async-http/ to download the latest android-async-http-1.4.9.jar, and then add this jar package to the Android application libs file Folder
(2) Import related classes by import com.loopj.android.http.*;
(3) Create asynchronous requests


The android-async-http open source framework allows us to easily obtain network data or send data to the server. The most important thing is that it is an asynchronous framework that uses a thread pool at the bottom to process concurrent requests, which is very efficient and easy to use.


    In the past, when we did projects on Android, such as downloading a lot of pictures, web pages or other resources, most developers would choose a model of one thread and one download task, because Android's own AndroidHttpClient or java.net.URL with java, The default is a blocking operation. This model is not efficient and is not suitable for apps with high concurrency requirements. Some people will choose to use nio to implement it themselves, and the code complexity is very high.


    As a core application class of the android-async-http framework, AsyncHttpClient is easy to use and can process web resources in various formats such as text and binary.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325528026&siteId=291194637