Evolving as a newborn, the United States aims to provide as many common network business processes as possible, while leaving developers with more customization and less bundling, enabling them to focus more on specific business logic without consuming too much cost for network request processing.

Making portal

Although the present is only a vision, but at least have a dream, although not necessarily realized, but who can guarantee, in case of realization?

EvolvingNetLib is a simple encapsulation of the most popular RxJava chain invocation, combined with RxJava2, Retrofit2, and OkHttp3, which simplifies the encapsulation of network operations.

function

EvolvingNetLib supports a variety of features, including: (1). Supports common Http requests, including GET, POST, PUT, DELETE, HEAD, and OPTIONS. (2). Memory cache search, disk cache search and network data request are supported. (3). Support memory cache and disk cache of data response. (4). Support file uploading and schedule callback. (5). Support file breakpoint download, progress callback. (6) support custom file download and save logic, in order to cope with some scenarios with strong combination with specific business, such as: gzip, chuncked, etc.

configuration

As with any open source library, to use EvolvingNetLib, you need to do the following:

1. Add the following configuration to the project root build.gradle:



maven { url 'https://jitpack.io' }
maven { url 'https://maven.google.com' }

2. In build. Gradle of app or Module, do the following:



Com. Making. CodingCodersCode. EvolvingNetLib: EvolvingNet: v1.0.0
Com. Making. CodingCodersCode. EvolvingNetLib: EvolvingBase: v1.0.0

3. The following permissions are required:

<! --> <uses-permission android:name="android.permission.INTERNET"/ > <! -- Create and delete file permissions in SD card --> <uses-permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS"/ > <! --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/ > <! -- Permission to write data to SD card --> <uses-permission Android :name="android.permission.WRITE_EXTERNAL_STORAGE" />Copy the code

Note: EvolvingNetLib does not provide any application logic for dynamic permission application in 6.0+ systems. Developers can choose their own application framework or implement their own dynamic permission application logic code to reduce unnecessary bundling and enhance customizability.

After the above configuration, you can use EvolvingNetLib to perform network request-related operations. For easy understanding and use, the following code examples illustrate.

Initialization Settings

If you want to actually make a network request, the first thing you need to do is initialize the library Settings. This is what any library needs. EvolvingNetLib does this:

CCRxNetManager ccRxNetManager = new CCRxNetManager.Builder()
                        .baseUrl("http://your host/") .callAdapterFactory(RxJava2CallAdapterFactory.create()) .converterFactory(GsonConverterFactory.create()) .commonHeaders(commonHeaderMap)// Set the public header information. ConnectTimeout (10, timeUnit.seconds). ReadTimeout (10, TimeUnit.SECONDS) .writeTimeout(10, TimeUnit.SECONDS) .enableLogInterceptor(true)// Whether to enable log print.build ();Copy the code

As you can see, all of the above initialization methods are the same as Retrofit, and in fact, all Settings are set to Retrofit.

Ordinary request

Class library initialization is complete, then you can make a real network request, take GET request as an example, complete use of the code as follows:

CCRxNetManager.<TestRespObj>get("/your api path").setheadermap (specifyHeaderMap)// Sets additional header information required by requests. SetPathMap (pathMap)// Used for PATH replacement in restful apis, Retrofit @path.setParamMap (paramMap)// Parameter information, In order to Map < String, SetRetryCount (3)// Number of network failed retries. SetRetryDelayTimeMillis (3000)// Failed retry delay SetCacheQueryMode (CCCacheMode. QueryMode. MODE_MEMORY_THEN_DISK_THEN_NET) / / the cache lookup strategy SetCacheSaveMode (CCCacheMode. SaveMode. MODE_SAVE_MEMORY_AND_DISK) / / cache the strategy setReqTag ("test_login_req_tag"// Request identity.setCacheKey ("test_login_req_cache_key"SetCCNetCallback (new RxNetManagerCallback())// Request callback.setCCCachesavecallBack (new) RxNetCacheSaveCallback())// Cache save callback.setccCacheQueryCallback (new RxNetCacheQueryCallback())// Cache lookup callback .setNetLifecycleComposer(this.<CCBaseResponse<TestRespObj>>bindUntilEvent(activityevent.destroy))//RxJava lifecycle management, See RxLifeCycle. SetResponseBeanType (TestRespobj.class)//json for the corresponding Java bean entity class.executeAsync(); // Execute the requestCopy the code

TestRespObj: The Java bean entity class corresponding to the requested target data, used as the second fromJson(String JSON,Type typeOfT) argument in Gson conversion.

SetCCCacheSaveCallback (): Data cache save callback. The reason why the class library does not provide the save logic is to reduce the binding, so that it is convenient for developers to customize the save mechanism. It is possible to use GreenDao, Litpal, or other open source database framework, or even customize the database operation. The data is completely controlled by the developer, so as to avoid the different needs of different developers. This makes the built-in save policy redundant. It also prevents some developers from feeling insecure about leaking data when the framework caches it.

SetCCCacheQueryCallback (): Cache data lookup callback. The purpose of doing this is the same as setCCCacheSaveCallback, which is to reduce bundling and give developers more customizability.

The rest of the methods are annotated in the above, I believe you can understand, I will not do too much tautology.

The code above shows GET requests. If you want to make other requests (POST, PUT, DELETE, HEAD, OPTIONS), Just replace get in ccrxNetManager.get (“/your API path”) with the corresponding lowercase form, such as POST, without changing anything else.

upload

The preceding section describes how to initiate a common request. The following section describes how to use an upload request. Without further ado, get right to the code.

CCRxNetManager.<String>upload("upload")
                    .setHeaderMap(specifyHeaderMap)
                    .setPathMap(pathMap)
                    .setTxtParamMap(txtParamMap)
                    .setFileParamMap(fileParamMap)
                    .setRetryCount(0)
                    .setCacheQueryMode(CCCacheMode.QueryMode.MODE_ONLY_NET)
                    .setCacheSaveMode(CCCacheMode.SaveMode.MODE_NO_CACHE)
                    .setReqTag("test_login_req_tag")
                    .setCacheKey("test_login_req_cache_key")
                    .setCCNetCallback(new RxNetUploadProgressCallback())
                    .setNetLifecycleComposer(this.<CCBaseResponse<String>>bindUntilEvent(ActivityEvent.DESTROY))
                    .setResponseBeanType(TestRespObj.class)
                    .executeAsync();Copy the code

The only difference is that the business logic parameters of the ordinary request are passed through setParamMap, while the parameters of the Upload request are passed through setTxtParamMap and setFileParamMap respectively. Both pass Map

objects, and as you can see from the method names, the former passes text class information and the latter file classpath information. At this point, the initiation of the upload request is complete, and all that remains is to listen for the result in the callback set by setCCNetCallback.
,>

download

As for the use of downloads, it is still directly to the code.

CCDownloadRequest downloadRequest = CCRxNetManager.<String>download("Sw - search - sp/software / 16 d5a98d3e034 / QQ_8 9.5.22062 _setup. Exe")
                        .setHeaderMap(specifyHeaderMap)
                        .setPathMap(pathMap)
                        .setFileSaveName("test_OkGo_apk_file_download.apk")
                        .setRetryCount(3)
                        .setCacheQueryMode(CCCacheMode.QueryMode.MODE_ONLY_NET)
                        .setCacheSaveMode(CCCacheMode.SaveMode.MODE_NO_CACHE)
                        .setReqTag("test_login_req_tag")
                        .setCacheKey("test_login_req_cache_key")
                        .setSupportRage(true)
                        .setDeleteExistFile(false)
                        .setCCNetCallback(new RxNetDownloadCalback())
                        .setNetLifecycleComposer(this.<CCBaseResponse<String>>bindUntilEvent(ActivityEvent.DESTROY))
                        .setResponseBeanType(TestRespObj.class);

downloadRequest.executeAsync();Copy the code

I don’t need to go into too much detail, but you can see from the method name the corresponding function of the method. To be sure, if you want to stop and stop the downloading and can pass downloadRequest. GetNetCCCanceler (). The cancel (); Method, if you want to continue to download operation, can be called again downloadRequest. ExecuteAsync ();

Well, that’s how EvolvingNetLib is used in its current version, and I believe it covers most network request operations. For those interested, go to Github to view and download the demo.

The demo does not cover all features at present, and will be improved one by one later.

If you like it, please give me a star to show your encouragement. If you don’t like it, please don’t troll. If you have good comments, suggestions or modifications, please make or submit a pull Request on Github, so that I can have the opportunity to learn from and worship god.

Making portal

Github address: github.com/CodingCoder…