Encapsulation of OkHttp
Since it is packaged, we can combine OKHttp and Gson, then we add the following dependencies to the gradle file
1 2 3 |
|
①CallBack creation
First of all, we know that when the interface request succeeds or fails, we need to notify the user of this information, then we need to create an abstract class RequestCallBack, the methods before the request, success, failure, and after the request, create OnBefore(), OnAfter(), OnError(), OnResponse() correspond
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Since the data we want is not necessarily every time, so here we use <T> to receive the data format we want to pretend, and get the desired data type (usually Bean, List) through reflection, so RequestCallBack The overall code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
② Simple encapsulation of Get and Post methods
First of all, we create a OkHttpClientManager class. Since it is a management class, we start with a singleton and a static object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
When creating the Manager object, we need to configure some parameters of OkHttp. By the way, since our asynchronous get and post methods run in sub-threads, here we add the distributed Handler mDelivery;, rewritten The construction method of OkHttpClientManager is as follows:
1 2 3 4 5 6 7 8 9 10 |
|
The previous external call object is encapsulated. Here we start to encapsulate the Get or Post method. Let me take the Post method as an example. First, analyze that the post method will have several parameters, the first parameter is url, the second parameter is params, Parameter three Callback (and our above RequestCallBack) parameter four flag (used to cancel the request operation, can be empty), the basic code is as follows:
1 2 3 4 5 6 7 8 9 10 |
|
Then let's take a look at what the deliveryResult method does
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
可以看到,这个方法主要是发出请求并对请求后的数据开始回调,这样我们就基本上封装好了一个post方法了 ,把代码这一部分的代码贴出来看看
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
这样我们就把Post方法封装好了,同理Get方法,ok,现在我们可以来调用调用了,在调用之前我们可以对返回数据格式再来封装封装,一般我们后台返回的数据格式是类似如下:
1 2 3 4 5 |
|
而data中有可能是对象,也有可能是数组,所以我们用两个类来实现一下
CommonResultBean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
CommonResultListBean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
ok,现在还是以上面我们登录的接口为例子开始我们的方法调用,返回的数据格式如图所示
我们创建UserMenu.java类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
|
所以MainActivity代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
这样我们就可以简单的调用了,最后看一下我们的效果:
See You Next Time···