OkHttpUtilss.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package com.kmall.manager.manager.common;
  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 OkHttpUtilss {
  12. public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
  13. public static final int CONNEC_TIME = 20;
  14. public static final int READ_TIME = 30;
  15. public static final int WRITE_TIME = 30;
  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. /**
  80. * 同步访问,返回Response
  81. * 可能超时
  82. * @param request
  83. * @return
  84. * @throws IOException
  85. */
  86. public static Response postReturnResponse(Request request) throws IOException {
  87. return okHttpClient.newCall(request).execute();
  88. }
  89. /**
  90. * 异步访问,回调结果
  91. * @param request
  92. * @param responseCallback
  93. */
  94. public static void asyncPostCallback(Request request, Callback responseCallback) {
  95. okHttpClient.newCall(request).enqueue(responseCallback);
  96. }
  97. /**
  98. * 异步访问,无结果返回
  99. * @param request
  100. */
  101. public static void asyncPost(Request request) {
  102. okHttpClient.newCall(request).enqueue(new Callback() {
  103. @Override
  104. public void onFailure(Call call, IOException e) {
  105. }
  106. @Override
  107. public void onResponse(Call call, Response response) throws IOException {
  108. }
  109. });
  110. }
  111. }