I'm working with java 8
I'm trying to do a HTTP POST request with a JSON body and then get the response as a string so I'll be able to parse it as Gson (JsonObject)
But !
When i use the HttpEntity
or the BasicResponseHandler
, i'm faced with this error : java.lang.NoClassDefFoundError: org/apache/http/HttpEntity
Here's the gradle dependency that i have
compile group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.4.13'
And here's the code of my Post request :
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;
}