OkHttpUtils.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package com.kmall.admin.cuspay.util;
  2. import okhttp3.*;
  3. import java.io.IOException;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.concurrent.TimeUnit;
  7. /**
  8. * @author Scott Chen
  9. * @date 2017/3/13
  10. */
  11. public class OkHttpUtils {
  12. public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
  13. public static final long CONNEC_TIME = 20L;
  14. public static final long READ_TIME = 30L;
  15. public static final long WRITE_TIME = 30L;
  16. private static OkHttpClient okHttpClient = null;
  17. static{
  18. okHttpClient = new OkHttpClient.Builder()
  19. .connectTimeout(CONNEC_TIME, TimeUnit.SECONDS)
  20. .readTimeout(READ_TIME, TimeUnit.SECONDS)
  21. .writeTimeout(WRITE_TIME, TimeUnit.SECONDS)
  22. .build();
  23. }
  24. /**
  25. * 构造RequestBody
  26. *
  27. * @param params
  28. * @return
  29. */
  30. public static RequestBody buildRequestBody(Map<String, String> params) {
  31. FormBody.Builder builder = new FormBody.Builder();
  32. Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
  33. while (iterator.hasNext()) {
  34. Map.Entry<String, String> entry = iterator.next();
  35. builder.add(entry.getKey(), entry.getValue());
  36. }
  37. return builder.build();
  38. }
  39. /**
  40. * 以字符串数据构建Request(未使用)
  41. * @param url
  42. * @param json
  43. * @return
  44. */
  45. public static Request buildRequest(String url, String json) {
  46. RequestBody body = RequestBody.create(JSON, json);
  47. return buildRequest(url, body);
  48. }
  49. /**
  50. * 构建Request
  51. * @param url
  52. * @param body
  53. * @return
  54. */
  55. public static Request buildRequest(String url, RequestBody body) {
  56. return new Request.Builder()
  57. .url(url)
  58. .post(body)
  59. .build();
  60. }
  61. /**
  62. * 同步访问,返回结果字符串
  63. * 可能超时
  64. *
  65. * @param request
  66. * @return
  67. * @throws IOException
  68. */
  69. public static String post(Request request) throws IOException {
  70. Response response = okHttpClient.newCall(request).execute();
  71. String result = "";
  72. if (response.isSuccessful()) {
  73. result = response.body().string();
  74. }else {
  75. throw new IOException("okhttp3 post exception: " + response);
  76. }
  77. return result;
  78. }
  79. public static String get(String url) throws IOException {
  80. Request request = (new Request.Builder()).get().url(url).build();
  81. Call call = okHttpClient.newCall(request);
  82. Response response = call.execute();
  83. return byte2String(response.body().bytes());
  84. }
  85. private static String byte2String(byte[] bytes) {
  86. return new String(bytes);
  87. }
  88. /**
  89. * 同步访问,返回Response
  90. * 可能超时
  91. *
  92. * @param request
  93. * @return
  94. * @throws IOException
  95. */
  96. public static Response postReturnResponse(Request request) throws IOException {
  97. return okHttpClient.newCall(request).execute();
  98. }
  99. /**
  100. * 异步访问,回调结果
  101. * @param request
  102. * @param responseCallback
  103. */
  104. public static void asyncPostCallback(Request request, Callback responseCallback) {
  105. okHttpClient.newCall(request).enqueue(responseCallback);
  106. }
  107. /**
  108. * 异步访问,无结果返回
  109. * @param request
  110. */
  111. public static void asyncPost(Request request) {
  112. okHttpClient.newCall(request).enqueue(new Callback() {
  113. @Override
  114. public void onFailure(Call call, IOException e) {
  115. }
  116. @Override
  117. public void onResponse(Call call, Response response) throws IOException {
  118. }
  119. });
  120. }
  121. }