我正在使用 java 8
我正在尝试使用 JSON 正文执行 HTTP POST 请求,然后将响应作为字符串获取,以便能够将其解析为 Gson (JsonObject)
但 !
当我使用HttpEntity
or 时BasicResponseHandler
,我遇到了这个错误:java.lang.NoClassDefFoundError: org/apache/http/HttpEntity
这是我拥有的 gradle 依赖项
compile group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.4.13'
这是我的 Post 请求的代码:
private static JsonObject execPostRequestTest(URL url, String jsonString) {
StringEntity stringEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(url.toString());
request.setEntity(stringEntity);
try {
HttpResponse httpResponse = httpClient.execute(request);
String responseString = new BasicResponseHandler().handleResponse(httpResponse);
return new JsonParser().parse(responseString).getAsJsonObject();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}