Specific usage of okhttputils developed by Android

1、 Overview

I wrote a full analysis of Android okhttp before. It's time to understand okhttp. In fact, it is mainly used as a popular article of okhttp. Of course, it also simply encapsulates the tool class. Unexpectedly, many people pay attention to and use it. Due to this enthusiasm, the methods in this tool class also increase sharply, and various overloaded methods make it extremely inconvenient to use. I'm really ashamed.

Therefore, this weekend, I took some time to disassemble and write the tool class again, improve the functions by the way, and improve its convenience and scalability as much as possible.

OK, at present, the package library supports:

Source address: https://github.com/hongyangAndroid/okhttp-utils

introduce:

Android Studio

Before use, users of Android studio can choose to add:

perhaps

2、 Basic usage

At present, the basic usage format is:

Through chain to add various parameters according to their needs, finally call execute (callback) to execute, and callback is asynchronous. If you simply execute (), it represents a synchronized method call.

You can see that the previous pile of get overloaded methods are cancelled, and the parameters can be flexibly selected.

Let's take a brief look at all the usage:

(1) Get request

(2) Post request

(3)Post String

Pass the string as the request body to the server, such as JSON string.

(4)Post File

Pass the file to the server as the request body

(5) Post based file upload (similar to forms on the web)

(6) Download File

(7) Display picture

Ha, for now, it's much clearer.

3、 Callback for upload and download

There is an inprogress method for the incoming callback. You need to get the progress and copy this method directly.

4、 For automatically resolving to entity classes

At present, gson's dependency is removed and a user-defined callback is provided to allow users to parse the returned data. At present, stringcallback, filecallback and bitmapcallback are provided to return string, download files and load pictures respectively.

Of course, if you want to resolve to an object, you can:

You can use your favorite JSON parsing library to complete it.

If it is resolved to list < user >, it is as follows:

5、 For HTTPS one-way authentication

Very simple, get the certificate of xxx.cert.

Then call

Recommended usage: for example, my certificate is placed in the assets Directory:

Just. Don't forget to register.

Note: if the HTTPS website is a certificate issued by an authority, the above settings are not required. Only self signed certificates are required.

6、 Disposition

(1) Global configuration

In the application, you can:

Then we call the various set methods of client.

For example:

(2) Set timeout for individual requests

For example, when it comes to files, you need to set more waiting time for reading and writing.

After calling build (), you can set various timeouts

(3) Cancel a single request

(4) Cancel request according to tag

At present, the last parameter object tag is added to all supported methods. Cancel is executed through okhttputils.canceltag (tag).

For example, in an activity, when the activity destroys the cancel request:

For example, all requests on the current activity page take the activity object as the tag, which can be cancelled uniformly in ondestory.

7、 On packaging

In fact, the whole encapsulation process is relatively simple. Here is a brief description. The process of a request for okhttp is roughly as follows:

The main difference is actually the construction process of request.

I abstract a class from request: okhttprequest

The construction of a request is divided into three steps: buildrequestbody, wrapprequestbody and buildrequest. When the above three methods are OK, we will get the request and execute it.

However, for different requests, the construction process of requestbody and request is different, so you can see that buildrequestbody and buildrequest are abstract methods, that is, different request classes, such as okhttpgetrequest and okhttppostrequest, need to construct their own requests.

For the wrapprequestbody method, you can see that it is basically empty by default, mainly because not all request classes need to copy it. Only when uploading, it needs to callback the progress and wrap the requestbody, so this method is similar to a hook.

In fact, this process is somewhat similar to the template method pattern.

For more detailed usage, you can check the readme and demo above GitHub. At present, the demo includes:

For the two buttons for uploading files, you need to build your own server, and the other buttons can be tested directly.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>