Commit cced7bb81e6d65a591b53c8b2e723db7da50e6cf
1 parent
5b4a61ad
Exists in
master
1.add function support json string post
Showing
2 changed files
with
15 additions
and
1 deletions
Show diff stats
build.gradle
src/main/java/com/taover/util/UtilHttpByOkHttp.java
... | ... | @@ -66,6 +66,16 @@ public class UtilHttpByOkHttp { |
66 | 66 | return getHttpClient(url).newCall(request).execute().body().string(); |
67 | 67 | } |
68 | 68 | |
69 | + public static String sendPostJson(String url, final Map<String, String> headers, String jsonStr) throws Exception{ | |
70 | + //请求体 | |
71 | + RequestBody requestBody = getReqeustBodyJson(jsonStr); | |
72 | + | |
73 | + //http头信息拼装 | |
74 | + Request request = getRequestBuilder(url, headers).post(requestBody).build(); | |
75 | + | |
76 | + return getHttpClient(url).newCall(request).execute().body().string(); | |
77 | + } | |
78 | + | |
69 | 79 | public static String sendDelete(String url, final Map<String, String> headers) throws Exception { |
70 | 80 | //http头信息拼装 |
71 | 81 | Request request = getRequestBuilder(url, headers).delete().build(); |
... | ... | @@ -110,6 +120,10 @@ public class UtilHttpByOkHttp { |
110 | 120 | return RequestBody.create(MediaType.parse("text/plain;charse=utf-8"), content); |
111 | 121 | } |
112 | 122 | |
123 | + private static RequestBody getReqeustBodyJson(String jsonStr){ | |
124 | + return RequestBody.create(MediaType.parse("application/json;charse=utf-8"), jsonStr); | |
125 | + } | |
126 | + | |
113 | 127 | private static Request.Builder getRequestBuilder(String url, Map<String, String> headers){ |
114 | 128 | //http头信息拼装 |
115 | 129 | Request.Builder requestBuilder = new Request.Builder().url(url); | ... | ... |