|
1 |
| -# RetrofitUtils |
| 1 | +* 欢迎关注微信公众号,长期推荐技术文章和技术视频 |
2 | 2 |
|
3 |
| -Retrofit封装框架,内部使用gson解析json数据 |
| 3 | +* 微信公众号名称:Android干货程序员 |
4 | 4 |
|
| 5 | + |
5 | 6 |
|
6 | 7 | PS:如果觉得文章太长,你也可观看该课程的[视频教程](https://v.qq.com/x/page/w0355urm511.html),亲,里面还有高清,无码的福利喔
|
| 8 | +>写在前面: |
| 9 | +Retrofit与okhttp共同出自于Square公司,是目前市场上使用最多的联网框架,retrofit是对okhttp做了一层封装,不过封装之后的retrofit上手还是极其复杂,为了解决使用难度问题,本文推荐使用github开源项目RetrofitUtils. |
7 | 10 |
|
8 |
| -* 欢迎关注微信公众号,长期推荐技术文章和技术视频 |
| 11 | +* RetrofitUtils开源项目地址:https://github.com/open-android/RetrofitUtils |
9 | 12 |
|
10 |
| -* 微信公众号名称:Android干货程序员 |
11 |
| - |
12 |
| - |
| 13 | +* 定义我们请求的Api,我们假设是这样的: |
| 14 | + |
| 15 | +* http://www.oschina.net/action/apiv2/banner?catalog=1 |
| 16 | + |
| 17 | +* 返回服务器的数据 |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | + { |
| 22 | + "code": 1, |
| 23 | + "message": "success", |
| 24 | + "result": { |
| 25 | + "items": [ |
| 26 | + { |
| 27 | + "detail": "", |
| 28 | + "href": "http://www.oschina.net/news/79650/2017-top-programming-languages", |
| 29 | + "id": 79650, |
| 30 | + "img": "https://static.oschina.net/uploads/cooperation/75410/google-beta-natural-language-api_048424e4-a8c3-41e8-91ef-aa009e3fc559.jpg", |
| 31 | + "name": "2017 年热门编程语言排行榜", |
| 32 | + "pubDate": "2016-12-06 11:51:24", |
| 33 | + "type": 6 |
| 34 | + }, |
| 35 | + { |
| 36 | + "detail": "", |
| 37 | + "href": "https://www.oschina.net/question/2720166_2210842", |
| 38 | + "id": 2210842, |
| 39 | + "img": "https://static.oschina.net/uploads/cooperation/75323/ubuntu-forum-black-sql_eb9137ea-efb2-49aa-99fd-025a221dcfe7.jpg", |
| 40 | + "name": "高手问答 | MySQL 开发和运维规范", |
| 41 | + "pubDate": "2016-12-06 15:48:10", |
| 42 | + "type": 2 |
| 43 | + }, |
| 44 | + { |
| 45 | + "detail": "", |
| 46 | + "href": "http://www.oschina.net/news/79757/tiobe-12", |
| 47 | + "id": 79757, |
| 48 | + "img": "https://static.oschina.net/uploads/cooperation/78083/chrome55-save-at-least-35-percent-memory_70ceba24-eb96-4710-99ec-c1cb5a26a3d6.jpg", |
| 49 | + "name": "C 语言为何一蹶不振?", |
| 50 | + "pubDate": "2016-12-08 15:18:20", |
| 51 | + "type": 6 |
| 52 | + }, |
| 53 | + { |
| 54 | + "detail": "", |
| 55 | + "href": "https://www.oschina.net/news/79732/firebug-stop-develope-and-maintain", |
| 56 | + "id": 79732, |
| 57 | + "img": "https://static.oschina.net/uploads/cooperation/77929/top-income-programming-languages-2016_16e9be1b-2a6b-453f-bafa-442fd043024b.jpg", |
| 58 | + "name": "Firebug 宣布不再维护,讲不出再见!", |
| 59 | + "pubDate": "2016-12-08 10:56:47", |
| 60 | + "type": 6 |
| 61 | + }, |
| 62 | + { |
| 63 | + "detail": "", |
| 64 | + "href": "http://www.oschina.net/news/79673/the-founder-of-cm-was-fired", |
| 65 | + "id": 79673, |
| 66 | + "img": "https://static.oschina.net/uploads/cooperation/78455/intellij-idea-2016-3-public-preview_63725513-45e4-4fb2-a0bf-c7940a7a87bc.jpg", |
| 67 | + "name": "Cyanogen 之父被踢出局", |
| 68 | + "pubDate": "2016-12-06 11:48:43", |
| 69 | + "type": 6 |
| 70 | + }], |
| 71 | + "nextPageToken": "61AF0C190D6BD629", |
| 72 | + "prevPageToken": "3EA621243546C8A5", |
| 73 | + "requestCount": 5, |
| 74 | + "responseCount": 5, |
| 75 | + "totalResults": 5 |
| 76 | + }, |
| 77 | + "time": "2016-12-13 10:56:41"} |
| 78 | + |
| 79 | +- 这样一个接口我看看使用RetrofitUtils该如何使用 |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +>大家看简单实用只需要new一个request然后发送一个send请求,就能返回服务器的数据,并且压根不需要解析拿到的直接就是各位需要的bean对象.那么我们来看看RetrofitUtils的使用步骤 |
13 | 85 |
|
14 |
| -开始 |
15 |
| -=== |
16 |
| -在project的build.gradle添加如下代码(如下图) |
17 |
| -```groovy |
18 |
| -allprojects { |
19 |
| - repositories { |
20 |
| - jcenter() |
21 |
| - maven { url "https://jitpack.io" } |
22 |
| - } |
23 |
| -} |
24 |
| -``` |
25 |
| - |
26 | 86 |
|
27 |
| -在build.gradle添加依赖 |
28 |
| -```groovy |
29 |
| - compile 'com.github.open-android:RetrofitUtils:0.4.13' |
30 |
| -``` |
31 |
| - |
32 |
| - |
33 |
| -### 需要的权限 |
34 |
| -```xml |
35 |
| -<uses-permission android:name="android.permission.INTERNET" /> |
36 |
| -<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> |
37 |
| -<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> |
38 |
| -<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> |
39 |
| -``` |
40 |
| - |
41 |
| -### 初始化 |
42 |
| -retrofitUtils初始化需要二个参数Context、baseUrl,最好在Application的onCreate()中初始化,记得在manifest.xml中注册Application。 |
43 |
| -```java |
44 |
| -ItheimaHttp.init(this, baseUrl);//baseUrl格式:"http://xxxxxx/xxxxx/" |
45 |
| -``` |
46 |
| - |
47 |
| -### 设置是否缓存http响应数据(默认支持缓存) |
48 |
| -```java |
49 |
| -ItheimaHttp.setHttpCache(false);//false不缓存,true缓存 |
50 |
| -``` |
51 |
| - |
52 |
| -### get/Post Bean类型异步请求(内部使用Gson解析json数据) |
53 |
| -```java |
54 |
| -//ItheimaHttp.newPostRequest(apiUrl) |
55 |
| -Request request = ItheimaHttp.newGetRequest(apiUrl);//apiUrl格式:"xxx/xxxxx" |
56 |
| -Call call = ItheimaHttp.send(request, new HttpResponseListener<Bean>() { |
57 |
| - @Override |
58 |
| - public void onResponse(Bean bean, Headers headers) { |
59 |
| - ........ |
60 |
| - } |
61 |
| - /** |
62 |
| - * 可以不重写失败回调 |
63 |
| - * @param call |
64 |
| - * @param e |
65 |
| - */ |
66 |
| - @Override |
67 |
| - public void onFailure(Call<ResponseBody> call, Throwable e) { |
68 |
| - ...... |
69 |
| - } |
70 |
| -}); |
71 |
| - |
72 |
| -//@param httpResponseListener 回调监听 |
73 |
| -//@param <T> Http响应数据泛型String或者Bean(使用String可以自己解析数据) |
74 |
| -//@return Call可以取消网络请求 |
75 |
| -``` |
76 |
| - |
77 |
| - |
78 |
| -```java |
79 |
| -Request request = ItheimaHttp.newGetRequest(apiUrl);//apiUrl格式:"xxx/xxxxx" |
80 |
| -Call call = ItheimaHttp.send(request, new HttpResponseListener<String>() { |
81 |
| - @Override |
82 |
| - public void onResponse(String string, Headers headers) { |
83 |
| - ........ |
84 |
| - } |
85 |
| - /** |
86 |
| - * 可以不重写失败回调 |
87 |
| - * @param call |
88 |
| - * @param e |
89 |
| - */ |
90 |
| - @Override |
91 |
| - public void onFailure(Call<ResponseBody> call, Throwable e) { |
92 |
| - ...... |
93 |
| - } |
94 |
| -}); |
95 |
| - |
96 |
| -//@param httpResponseListener 回调监听 |
97 |
| -//@param <T> Http响应数据泛型String或者Bean(使用String可以自己解析数据) |
98 |
| -//@return Call可以取消网络请求 |
99 |
| -``` |
100 |
| - |
101 |
| -### 添加请求参数 |
102 |
| -```java |
103 |
| -request.putParams(key,value) |
104 |
| -.putParams(key,value) |
105 |
| -.putParams(key,value); |
106 |
| - |
107 |
| -Map<String,Object> map = new HashMap<>(); |
108 |
| -map.put(key,value); |
109 |
| -request.putParamsMap(map); |
110 |
| -``` |
111 |
| -###添加请求头 |
112 |
| -```java |
113 |
| -//添加请求头 |
114 |
| -request.putHeader(key,value) |
115 |
| -.putHeader(key,value); |
116 |
| -``` |
117 |
| - |
118 |
| -###get/post String类型异步请求 |
119 |
| -```java |
120 |
| -Call call = ItheimaHttp.getAsync(apiUrl, new HttpResponseListener<String>); |
121 |
| - |
122 |
| -Call call = ItheimaHttp.postAsync(apiUrl, new HttpResponseListener<String>() { |
123 |
| - @Override |
124 |
| - public void onResponse(String s, Headers headers) { |
125 |
| - ........ |
126 |
| - } |
127 |
| -}); |
128 |
| -``` |
129 |
| - |
130 |
| -###get/post Bean类型异步请求,内部使用Gson解析json数据 |
131 |
| -```java |
132 |
| -Call call = ItheimaHttp.getAsync(apiUrl, new HttpResponseListener<Bean>); |
133 |
| - |
134 |
| -Call call = ItheimaHttp.postAsync(apiUrl, new HttpResponseListener<Bean>() { |
135 |
| - @Override |
136 |
| - public void onResponse(Bean bean, Headers headers) { |
137 |
| - ........ |
138 |
| - } |
139 |
| -}); |
140 |
| -``` |
141 |
| - |
142 |
| -###文件上传 |
143 |
| -```java |
144 |
| -Request request = ItheimaHttp.newUploadRequest("http://xxxxxx/xxxx", RequestMethod.POST); |
145 |
| - request.putUploadFile("resource", mSdFile) |
146 |
| - .putHeader("cookie", cookie) |
147 |
| - .putMediaType(MediaType.parse("application/octet-stream")); |
148 |
| -ItheimaHttp.upload(request, new UploadListener() { |
149 |
| - @Override |
150 |
| - public void onResponse(Call call, Response response) { |
151 |
| - //上传成功回调 |
152 |
| - } |
| 87 | + |
| 88 | +> **在project的build.gradle添加如下代码(如下图)** |
| 89 | +
|
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +>**引入当前项目** |
| 94 | +
|
| 95 | + compile 'com.github.open-android:RetrofitUtils:0.1.9' |
| 96 | + |
| 97 | +>**在清单文件添加如下权限** |
| 98 | +
|
| 99 | + <uses-permission android:name="android.permission.INTERNET" /> |
| 100 | + <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> |
| 101 | + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> |
| 102 | + |
| 103 | +>**新建Application类并且在oncreate方法当中进行初始化: |
| 104 | +千万注意Application记得在manifest.xml中注册。** |
| 105 | + |
| 106 | + |
| 107 | + ItheimaHttp.init(this, Urls.getBaseUrl()); |
| 108 | + |
| 109 | +eg:请求地址:http://www.oschina.net/action/apiv2/banner?catalog=1 |
| 110 | + |
| 111 | +那么baseUrl = http://www.oschina.net |
| 112 | +RetrofitUtils内部封装了如下使用方法 |
| 113 | + |
| 114 | +>看一个完整Get和Post请求是如何实现:异步请求(内部使用Gson解析json数据)直接返回实体bean对象 |
| 115 | +
|
153 | 116 |
|
154 |
| - @Override |
155 |
| - public void onProgress(long progress, long total, boolean done) { |
156 |
| - //上传进度回调progress:上传进度,total:文件长度, done:上传是否完成 |
157 |
| - } |
158 | 117 |
|
159 |
| - @Override |
160 |
| - public void onFailure(Call call, Throwable t) { |
161 |
| - //上传失败 |
162 |
| - } |
163 |
| -}); |
164 |
| -``` |
| 118 | + Request request = ItheimaHttp.newGetRequest(apiUrl);//apiUrl格式:"xxx/xxxxx" |
| 119 | + Call call = ItheimaHttp.send(request, new HttpResponseListener<Bean>() { |
| 120 | + @Override |
| 121 | + public void onResponse(Bean bean) { |
| 122 | + ........ |
| 123 | + } |
| 124 | + /** |
| 125 | + * 可以不重写失败回调 |
| 126 | + * @param call |
| 127 | + * @param e |
| 128 | + */ |
| 129 | + @Override |
| 130 | + public void onFailure(Call<ResponseBody> call, Throwable e) { |
| 131 | + ...... |
| 132 | + } |
| 133 | + }); |
| 134 | + @param httpResponseListener 回调监听 |
| 135 | + @param <T> Bean |
| 136 | + @return Call可以取消网络请求</span> |
| 137 | + |
| 138 | +>添加请求参数 |
| 139 | +
|
| 140 | + request.putParams(key,value) |
| 141 | + .putParams(key,value) |
| 142 | + .putParams(key,value); |
| 143 | + |
| 144 | + |
| 145 | + Map<String,Object> map = new HashMap<>(); |
| 146 | + map.put(key,value); |
| 147 | + request.putParamsMap(map); |
165 | 148 |
|
166 |
| -###取消网络请求 |
167 |
| -```java |
168 |
| -call.cancel(); |
169 |
| -``` |
| 149 | +>添加请求头 |
170 | 150 |
|
171 |
| -###是否需要查看日志 |
172 |
| -```java |
173 |
| -ItheimaHttp.setDebug(true); |
174 |
| -``` |
| 151 | + request.putHeader(key,value) |
| 152 | + .putHeader(key,value); |
175 | 153 |
|
176 |
| -* 详细的使用方法在DEMO里面都演示啦,如果你觉得这个库还不错,请赏我一颗star吧~~~ |
| 154 | +>如果不需要RetrofitUtils框架自动解析json,那么可以使用如下请求 |
177 | 155 |
|
178 |
| -* 欢迎关注微信公众号 |
| 156 | + Call call = ItheimaHttp.getAsync(apiUrl, new HttpResponseListener<String>); |
| 157 | + |
| 158 | + Call call = ItheimaHttp.postAsync(apiUrl, new HttpResponseListener<String>() { |
| 159 | + @Override |
| 160 | + public void onResponse(String s) { |
| 161 | + ........ |
| 162 | + } |
| 163 | + }); |
179 | 164 |
|
180 |
| - |
| 165 | +>如果需要RetrofitUtils框架自动解析json,直接获取javabean,那么可以使用如下请求 |
181 | 166 |
|
182 |
| -[回到顶部](#readme) |
| 167 | + Call call = ItheimaHttp.getAsync(apiUrl, new HttpResponseListener<Bean>); |
| 168 | + |
| 169 | + Call call = ItheimaHttp.postAsync(apiUrl, new HttpResponseListener<Bean>() { |
| 170 | + @Override |
| 171 | + public void onResponse(Bean bean) { |
| 172 | + ........ |
| 173 | + } |
| 174 | + }); |
| 175 | + |
| 176 | +>文件上传 |
| 177 | + |
| 178 | + Request request = ItheimaHttp.newUploadRequest("http://xxx/xxx/xxx", RequestMethod.POST); |
| 179 | + request.putUploadFile(uploadFile).putMediaType(MediaType.parse("multipart/form-data")); |
| 180 | + ItheimaHttp.upload(request, new UploadListener() { |
| 181 | + @Override |
| 182 | + public void onResponse(Call call, Response response) { |
| 183 | + //上传成功回调 |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public void onProgress(long progress, long total, boolean done) { |
| 188 | + //上传进度回调progress:上传进度,total:文件长度, done:上传是否完成 |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public void onFailure(Call call, Throwable t) { |
| 193 | + //上传失败 |
| 194 | + } |
| 195 | + }); |
| 196 | + |
| 197 | +>取消网络请求 |
| 198 | +
|
| 199 | + call.cancel(); |
0 commit comments