Commit cced7bb81e6d65a591b53c8b2e723db7da50e6cf

Authored by 王彬
1 parent 5b4a61ad
Exists in master

1.add function support json string post

build.gradle
... ... @@ -54,7 +54,7 @@ uploadArchives {
54 54 authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
55 55 }
56 56 pom.project {
57   - version '1.1.3'
  57 + version '1.1.4'
58 58 artifactId ARTIFACT_Id
59 59 groupId GROUP_ID
60 60 packaging TYPE
... ...
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);
... ...