项目作者: caiyc0818

项目描述 :
结合okhttputils 和retrofitutil 封装的的retrofit请求工具类
高级语言: Java
项目地址: git://github.com/caiyc0818/retrofitUtils.git
创建时间: 2018-09-04T02:30:18Z
项目社区:https://github.com/caiyc0818/retrofitUtils

开源协议:

下载


RetrofitUtils

  1. Android Studio
  2. //retrofit
  3. compile 'com.squareup.okio:okio:1.14.0'
  4. compile 'com.squareup.okhttp3:okhttp:3.10.0'
  5. compile 'com.squareup.retrofit2:retrofit:2.4.0'
  6. compile 'com.squareup.retrofit2:converter-scalars:2.3.0'

目前对以下需求进行了封装

  1. 一般的get请求
  2. 一般的post请求
  3. 基于Http Post的文件上传(类似表单)
  4. 文件下载/加载图片
  5. 上传下载的进度回调
  6. 支持取消某个请求
  7. 支持自定义Callback
  8. 支持HEADDELETEPATCHPUT

配置RetrofitUtils

public class MyApplication extends Application
{
@Override
public void onCreate()
{
super.onCreate();

  1. OkHttpClient okHttpClient = new OkHttpClient.Builder()
  2. .connectTimeout(20000L, TimeUnit.MILLISECONDS)
  3. .readTimeout(20000L, TimeUnit.MILLISECONDS)
  4. .addInterceptor(new LoggingInterceptor())
  5. .build();
  6. HttpCreator.init(Urls.URL_BASE_HOST, okHttpClient);
  7. }

}

其他用法示例

  1. /**
  2. * get 请求模拟
  3. *
  4. * @param view
  5. */
  6. public void get(View view) {
  7. RetrofitUtils.get()
  8. .url("query")
  9. .addParams("name", "bbbb")
  10. .build()
  11. .execute(new StringCallBack() {
  12. @Override
  13. public void onError(Call call, Throwable e,int id) {
  14. toast(e.getMessage());
  15. }
  16. @Override
  17. public void onResponse(Call call, String response,int id) {
  18. toast(response);
  19. }
  20. });
  21. }
  22. /**
  23. * 模拟 post 请求
  24. *
  25. * @param view
  26. */
  27. public void post(View view) {
  28. RetrofitUtils.post()
  29. .url("postParam")
  30. .addParams("name", "bbbb")
  31. .build()
  32. .execute(new StringCallBack() {
  33. @Override
  34. public void onError(Call call, Throwable e,int id) {
  35. toast(e.getMessage());
  36. }
  37. @Override
  38. public void onResponse(Call call, String response,int id) {
  39. toast(response);
  40. }
  41. });
  42. }
  43. /**
  44. * 模拟 postRaw 请求
  45. *
  46. * @param view
  47. */
  48. public void postRaw(View view) {
  49. HashMap hashMap = new HashMap();
  50. hashMap.put("fileName", "qinlei");
  51. RetrofitUtils.postRaw()
  52. .url("postRaw")
  53. .addBody(JSON.toJSONString(hashMap))
  54. .build()
  55. .execute(new StringCallBack() {
  56. @Override
  57. public void onError(Call call, Throwable e,int id) {
  58. toast(e.getMessage());
  59. }
  60. @Override
  61. public void onResponse(Call call, String response,int id) {
  62. toast(response);
  63. Log.d("qinlei", "onResponse: "+response);
  64. }
  65. });
  66. String xml = "<xml></xml>";
  67. RetrofitUtils.postRaw()
  68. .url("https://api.mch.weixin.qq.com/pay/unifiedorder")
  69. .addBody(xml, "application/xml;charset=UTF-8")
  70. .build()
  71. .execute(new StringCallBack() {
  72. @Override
  73. public void onError(Call call, Throwable e,int id) {
  74. toast(e.getMessage());
  75. }
  76. @Override
  77. public void onResponse(Call call, String response,int id) {
  78. toast(response);
  79. Log.d("qinlei", "onResponse: "+response);
  80. }
  81. });
  82. }
  83. /**
  84. * 模拟 put 请求
  85. *
  86. * @param view
  87. */
  88. public void put(View view) {
  89. RetrofitUtils.put()
  90. .url("putParam")
  91. .tag(this)
  92. .addParams("name", "bbbb")
  93. .build()
  94. .execute(new StringCallBack() {
  95. @Override
  96. public void onError(Call call, Throwable e,int id) {
  97. toast(e.getMessage());
  98. }
  99. @Override
  100. public void onResponse(Call call, String response,int id) {
  101. toast(response);
  102. }
  103. });
  104. }
  105. /**
  106. * 模拟 putRaw 请求
  107. *
  108. * @param view
  109. */
  110. public void putRaw(View view) {
  111. HashMap hashMap = new HashMap();
  112. hashMap.put("fileName", "qinlei");
  113. RetrofitUtils.putRaw()
  114. .url("putRaw")
  115. .addBody(JSON.toJSONString(hashMap))
  116. .build()
  117. .execute(new StringCallBack() {
  118. @Override
  119. public void onError(Call call, Throwable e,int id) {
  120. toast(e.getMessage());
  121. }
  122. @Override
  123. public void onResponse(Call call, String response,int id) {
  124. toast(response);
  125. }
  126. });
  127. }
  128. /**
  129. * 模拟 delete 请求
  130. *
  131. * @param view
  132. */
  133. public void delete(View view) {
  134. toast("没有测试的服务器");
  135. }
  136. /**
  137. * 模拟 upload 请求
  138. *
  139. * @param view
  140. */
  141. public void upload(View view) {
  142. RetrofitUtils.upload()
  143. .url("http://192.168.31.236/TruckAlliance/userInterface/v1/uploadBatch")
  144. .addFiles("file", file)
  145. .addFiles("file", file)
  146. .build()
  147. .execute(new StringCallBack() {
  148. ProgressDialog progressDialog;
  149. @Override
  150. public void onBefore(Call call) {
  151. super.onBefore(call);
  152. progressDialog = new ProgressDialog(MainActivity.this);
  153. progressDialog.setTitle("上传图片");
  154. progressDialog.setMessage("上传图片");
  155. progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  156. progressDialog.setCancelable(false);
  157. progressDialog.setCanceledOnTouchOutside(false);
  158. progressDialog.show();
  159. }
  160. @Override
  161. public void inProgress(float progress, long progressMax) {
  162. super.inProgress(progress, progressMax);
  163. progressDialog.setMax((int) progressMax);
  164. progressDialog.setProgress((int) progress);
  165. }
  166. @Override
  167. public void onError(Call call, Throwable e,int id) {
  168. toast(e.getMessage());
  169. }
  170. @Override
  171. public void onResponse(Call call, String response,int id) {
  172. Log.d("qinlei", "onResponse: " + response);
  173. toast("上传完成");
  174. }
  175. @Override
  176. public void onAfter(Call call) {
  177. super.onAfter(call);
  178. progressDialog.dismiss();
  179. }
  180. });
  181. }
  182. }
  183. /**
  184. * 模拟 download 请求
  185. *
  186. * @param view
  187. */
  188. public void download(View view) {
  189. //需要请求权限
  190. if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
  191. download();
  192. } else {
  193. ActivityCompat.requestPermissions(this,
  194. new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
  195. 1000);
  196. }
  197. }
  198. public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
  199. switch (requestCode) {
  200. case 1000: {
  201. if (grantResults.length > 0
  202. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  203. download();
  204. } else {
  205. }
  206. return;
  207. }
  208. }
  209. }
  210. private void download() {
  211. RetrofitUtils
  212. .download()
  213. .url("http:\\\\zhstatic.zhihu.com\\pkg\\store\\daily\\zhihu-daily-zhihu-2.6.0(744)-release.apk")
  214. .build()
  215. .execute(new BaseFileCallBack(DOWNLOAD_SERVICE, "知乎日报.apk") {
  216. ProgressDialog progressDialog;
  217. @Override
  218. public void onBefore(Call call) {
  219. super.onBefore(call);
  220. progressDialog = new ProgressDialog(MainActivity.this);
  221. progressDialog.setTitle("下载apk");
  222. progressDialog.setMessage("下载");
  223. progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  224. progressDialog.setCancelable(false);
  225. progressDialog.setCanceledOnTouchOutside(false);
  226. progressDialog.show();
  227. }
  228. @Override
  229. public void inProgress(float progress, long progressMax) {
  230. super.inProgress(progress, progressMax);
  231. progressDialog.setMax((int) progressMax);
  232. progressDialog.setProgress((int) progress);
  233. }
  234. @Override
  235. public void onError(Call call, Throwable e,int id) {
  236. toast(e.getMessage());
  237. }
  238. @Override
  239. public void onResponse(Call call, File response,int id) {
  240. toast("下载完成");
  241. }
  242. @Override
  243. public void onAfter(Call call) {
  244. super.onAfter(call);
  245. progressDialog.dismiss();
  246. }
  247. });
  248. }

取消所有请求

RetrofitUtils.cancel();

根据tag取消请求

目前对于支持的方法都添加了最后一个参数Object tag,取消则通过RetrofitUtils.cancelTag(tag)执行。

例如:在Activity中,当Activity销毁取消请求:

@Override

protected void onDestroy()

{

  1. super.onDestroy();
  2. //可以取消同一个tag的
  3. RetrofitUtils.cancelTag(this);//取消以Activity.this作为tag的请求

}

比如,当前Activity页面所有的请求以Activity对象作为tag,可以在onDestory里面统一取消。
混淆

OkHttp3

-dontwarn okhttp3.logging.**

-keep class okhttp3.internal.*{;}

-dontwarn okio.**

Retrofit

-dontwarn retrofit2.**

-keep class retrofit2.* { ; }

-keepattributes Signature

-keepattributes Exceptions