OKHttpUpdateHttpService.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com)
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.yijia.handpos.http;
  17. import androidx.annotation.NonNull;
  18. import com.xuexiang.xupdate.proxy.IUpdateHttpService;
  19. import com.xuexiang.xupdate.utils.UpdateUtils;
  20. import com.zhy.http.okhttp.OkHttpUtils;
  21. import com.zhy.http.okhttp.callback.FileCallBack;
  22. import com.zhy.http.okhttp.callback.StringCallback;
  23. import com.zhy.http.okhttp.request.RequestCall;
  24. import java.io.File;
  25. import java.util.Map;
  26. import java.util.TreeMap;
  27. import okhttp3.Call;
  28. import okhttp3.MediaType;
  29. import okhttp3.Request;
  30. /**
  31. * 使用okhttp
  32. *
  33. * @author xuexiang
  34. * @since 2018/7/10 下午4:04
  35. */
  36. public class OKHttpUpdateHttpService implements IUpdateHttpService {
  37. private boolean mIsPostJson;
  38. public OKHttpUpdateHttpService() {
  39. this(false);
  40. }
  41. public OKHttpUpdateHttpService(boolean isPostJson) {
  42. mIsPostJson = isPostJson;
  43. }
  44. @Override
  45. public void asyncGet(@NonNull String url, @NonNull Map<String, Object> params, final @NonNull Callback callBack) {
  46. OkHttpUtils.get()
  47. .url(url)
  48. .params(transform(params))
  49. .build()
  50. .execute(new StringCallback() {
  51. @Override
  52. public void onError(Call call, Exception e, int id) {
  53. callBack.onError(e);
  54. }
  55. @Override
  56. public void onResponse(String response, int id) {
  57. callBack.onSuccess(response);
  58. }
  59. });
  60. }
  61. @Override
  62. public void asyncPost(@NonNull String url, @NonNull Map<String, Object> params, final @NonNull Callback callBack) {
  63. //这里默认post的是Form格式,使用json格式的请修改 post -> postString
  64. RequestCall requestCall;
  65. if (mIsPostJson) {
  66. requestCall = OkHttpUtils.postString()
  67. .url(url)
  68. .content(UpdateUtils.toJson(params))
  69. .mediaType(MediaType.parse("application/json; charset=utf-8"))
  70. .build();
  71. } else {
  72. requestCall = OkHttpUtils.post()
  73. .url(url)
  74. .params(transform(params))
  75. .build();
  76. }
  77. requestCall
  78. .execute(new StringCallback() {
  79. @Override
  80. public void onError(Call call, Exception e, int id) {
  81. callBack.onError(e);
  82. }
  83. @Override
  84. public void onResponse(String response, int id) {
  85. callBack.onSuccess(response);
  86. }
  87. });
  88. }
  89. @Override
  90. public void download(@NonNull String url, @NonNull String path, @NonNull String fileName, final @NonNull DownloadCallback callback) {
  91. OkHttpUtils.get()
  92. .url(url)
  93. .tag(url)
  94. .build()
  95. .execute(new FileCallBack(path, fileName) {
  96. @Override
  97. public void inProgress(float progress, long total, int id) {
  98. callback.onProgress(progress, total);
  99. }
  100. @Override
  101. public void onError(Call call, Exception e, int id) {
  102. callback.onError(e);
  103. }
  104. @Override
  105. public void onResponse(File response, int id) {
  106. callback.onSuccess(response);
  107. }
  108. @Override
  109. public void onBefore(Request request, int id) {
  110. super.onBefore(request, id);
  111. callback.onStart();
  112. }
  113. });
  114. }
  115. @Override
  116. public void cancelDownload(@NonNull String url) {
  117. OkHttpUtils.getInstance().cancelTag(url);
  118. }
  119. private Map<String, String> transform(Map<String, Object> params) {
  120. Map<String, String> map = new TreeMap<>();
  121. for (Map.Entry<String, Object> entry : params.entrySet()) {
  122. map.put(entry.getKey(), entry.getValue().toString());
  123. }
  124. return map;
  125. }
  126. }