|
| 1 | +#RetrofitUtils |
| 2 | + |
| 3 | +Retrofit封装框架,内部使用gson解析json数据 |
| 4 | + |
| 5 | + |
| 6 | + PS:如果觉得文章太长,你也可观看该课程的[视频](https://www.boxuegu.com/web/html/video.html?courseId=172§ionId=8a2c9bed5a3a4c7e015a3ad9a490030d&chapterId=8a2c9bed5a3a4c7e015a3ad9dfdf030e&vId=8a2c9bed5a3a4c7e015a3adaaaa9030f&videoId=4DC518DB11BC473E9C33DC5901307461),亲,里面还有高清,无码的福利喔 |
| 7 | + |
| 8 | + |
| 9 | +* 爱生活,爱学习,更爱做代码的搬运工,分类查找更方便请下载黑马助手app |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 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 | + |
| 27 | +在build.gradle添加依赖 |
| 28 | +```groovy |
| 29 | +compile 'com.github.open-android:RetrofitUtils:0.3.12' |
| 30 | +``` |
| 31 | + |
| 32 | +###需要的权限 |
| 33 | +```xml |
| 34 | +<uses-permission android:name="android.permission.INTERNET" /> |
| 35 | +<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> |
| 36 | +<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> |
| 37 | +<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> |
| 38 | +``` |
| 39 | + |
| 40 | +###初始化 |
| 41 | +retrofitUtils初始化需要二个参数Context、baseUrl,最好在Application的onCreate()中初始化,记得在manifest.xml中注册Application。 |
| 42 | +```java |
| 43 | +ItheimaHttp.init(this, baseUrl);//baseUrl格式:"http://xxxxxx/xxxxx/" |
| 44 | +``` |
| 45 | + |
| 46 | +###设置是否缓存http响应数据(默认支持缓存) |
| 47 | +```java |
| 48 | +ItheimaHttp.setHttpCache(false);//false不缓存,true缓存 |
| 49 | +``` |
| 50 | + |
| 51 | +###get/Post Bean类型异步请求(内部使用Gson解析json数据) |
| 52 | +```java |
| 53 | +//ItheimaHttp.newPostRequest(apiUrl) |
| 54 | +Request request = ItheimaHttp.newGetRequest(apiUrl);//apiUrl格式:"xxx/xxxxx" |
| 55 | +Call call = ItheimaHttp.send(request, new HttpResponseListener<Bean>() { |
| 56 | + @Override |
| 57 | + public void onResponse(Bean bean, Headers headers) { |
| 58 | + ........ |
| 59 | + } |
| 60 | + /** |
| 61 | + * 可以不重写失败回调 |
| 62 | + * @param call |
| 63 | + * @param e |
| 64 | + */ |
| 65 | + @Override |
| 66 | + public void onFailure(Call<ResponseBody> call, Throwable e) { |
| 67 | + ...... |
| 68 | + } |
| 69 | +}); |
| 70 | + |
| 71 | +//@param httpResponseListener 回调监听 |
| 72 | +//@param <T> Http响应数据泛型String或者Bean(使用String可以自己解析数据) |
| 73 | +//@return Call可以取消网络请求 |
| 74 | +``` |
| 75 | + |
| 76 | + |
| 77 | +```java |
| 78 | +Request request = ItheimaHttp.newGetRequest(apiUrl);//apiUrl格式:"xxx/xxxxx" |
| 79 | +Call call = ItheimaHttp.send(request, new HttpResponseListener<String>() { |
| 80 | + @Override |
| 81 | + public void onResponse(String string, Headers headers) { |
| 82 | + ........ |
| 83 | + } |
| 84 | + /** |
| 85 | + * 可以不重写失败回调 |
| 86 | + * @param call |
| 87 | + * @param e |
| 88 | + */ |
| 89 | + @Override |
| 90 | + public void onFailure(Call<ResponseBody> call, Throwable e) { |
| 91 | + ...... |
| 92 | + } |
| 93 | +}); |
| 94 | + |
| 95 | +//@param httpResponseListener 回调监听 |
| 96 | +//@param <T> Http响应数据泛型String或者Bean(使用String可以自己解析数据) |
| 97 | +//@return Call可以取消网络请求 |
| 98 | +``` |
| 99 | + |
| 100 | +###添加请求参数 |
| 101 | +```java |
| 102 | +request.putParams(key,value) |
| 103 | +.putParams(key,value) |
| 104 | +.putParams(key,value); |
| 105 | + |
| 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 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public void onProgress(long progress, long total, boolean done) { |
| 156 | + //上传进度回调progress:上传进度,total:文件长度, done:上传是否完成 |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public void onFailure(Call call, Throwable t) { |
| 161 | + //上传失败 |
| 162 | + } |
| 163 | +}); |
| 164 | +``` |
| 165 | + |
| 166 | +###取消网络请求 |
| 167 | +```java |
| 168 | +call.cancel(); |
| 169 | +``` |
| 170 | + |
| 171 | +###是否需要查看日志 |
| 172 | +```java |
| 173 | +ItheimaHttp.setDebug(true); |
| 174 | +``` |
| 175 | + |
| 176 | +* 详细的使用方法在DEMO里面都演示啦,如果你觉得这个库还不错,请赏我一颗star吧~~~ |
| 177 | + |
| 178 | +* 欢迎关注微信公众号 |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | +[回到顶部](#readme) |
0 commit comments