Simple use and packaging of Android okhttp
Simple use and packaging of Android okhttp
1. I took a closer look at okhttp yesterday. In the past, I used to call the network framework encapsulated by my colleagues, which is easy to use directly, but my own encapsulation is not so simple. Fortunately, today comes from my redemption, and I will write with you about the use of simple get and post of okhttp, the most basic okhttp, and then its encapsulation.
2. Simple use of okhttp
First, we create a project and add three controls to the layout file, textview (used to display the information obtained from JSON), button (click start to request network), and ProgressBar (network loading prompt box)
① simple asynchronous get request
The first step is to create the okhttpclient object
Step 2: create a request
Step 3: create a call object
Step 4: add the request to the schedule
Not much to say, go directly to the code:
The effects are as follows:
Note that since the enqueue () method we call runs in the network thread, if we want to get the updated UI after we get the JSON data, we can use the handle. Post () method to update the UI in the run method.
② simple asynchronous post request
The post request here takes the most common registration login as an example. The steps of post request are similar to those of get, except that the parameters required by the server are passed in when creating the request
The code is as follows:
Take a look at the breakpoint of our server
You can see that our server did get the parameters we passed, and then look at the data we got after our request
OK, then our post method will be OK
3. Okhttp package
Since it is encapsulation, we can combine okhttp and gson, so we add the following dependencies to the gradle file
① creation of callback
First, we know that when the interface request succeeds or fails, we need to notify the user of this information, so we need to create an abstract class requestcallback, pre request, success, failure and post request methods, and create onbefore(), onafter(), onerror(), onresponse()
Because the data we want each time is not necessarily, here we use < T > to receive the data format we want to install, and get the desired data types (generally bean and list) through reflection. Therefore, the overall code of requestcallback is as follows:
② simple encapsulation of get and post methods
First, we create an okhttpclientmanager class. Because it is a management class, it starts with a single instance plus a static object
When creating the manager object, we need to configure some parameters of okhttp. By the way, because our asynchronous get and post methods run in sub threads, we add the distributed handler mdelivery;, The construction method of the rewritten okhttpclientmanager is as follows:
The previous external call object is encapsulated. Here we begin to encapsulate the get or post method. I take the post method as an example. First, analyze that the post method will have several parameters, parameter 1 URL, parameter 2 params, parameter 3 callback (and our above requestcallback) and parameter 4 flag (used to cancel the request operation, which can be empty), The basic code is as follows:
Then let's take a look at what the deliveryresult method does
As you can see, this method is mainly to send a request and start callback on the data after the request. In this way, we basically encapsulate a post method and post the code in this part of the code
In this way, we can encapsulate the post method. Similarly to the get method, OK, now we can call it. Before calling, we can encapsulate the returned data format. Generally, the data format returned in the background is similar to the following:
Data may be an object or an array, so we use two classes to implement it
CommonResultBean
CommonResultListBean
OK, let's start our method call with the above login interface as an example, and the returned data format is shown in the figure
We create the usermenu. Java class
Therefore, the mainactivity code is as follows:
In this way, we can simply call. Finally, let's take a look at our effect:
See You Next Time・・・
Thank you for reading, hope to help you, thank you for your support to this site!