sigillum_conf sigillum_conf - 1 month ago 5
Apache Configuration Question

Sending JSON to Java post and displaying info

I am trying to send the following JSON string to a url in Java:

Where tag name is

data
and the body is

{"process": "mobileS","phone": "9999999999"}


The code that I have so far is the following:

HttpClient httpClient = HttpClientBuilder.create().build(); //Use this instead

try {
HttpPost request = new HttpPost("url goes here");
StringEntity params = new StringEntity("details={\"process\":\"mobileS\",\"phone\":\"9999999999\"}");
request.addHeader("content-type", "application/x-www-form-urlencoded");
request.setEntity(params);

HttpResponse response = httpClient.execute(request);
System.out.println(response);

// handle response here...
} catch (Exception ex) {
// handle exception here
} finally {
httpClient.getConnectionManager().shutdown(); //Deprecated
}


I am supposed to get a JSON string from the above after sending that JSON data to the server, but I don't know where to go after sending the above request.

The good thing is that the response gives back a 200 response and it shows all the other information that would normally come from the result of HttpResponse, but I am to get a result similar to:

{"test": {"this is":"what I am supposed to get"}}


So basically it should return a JSON string and I am getting something completely different from what I need

HttpResponseProxy{HTTP/1.1 200 OK [Cache-Control: no-store, no-cache, must-revalidate, max-age=0,post-check=0, pre-check=0 etc etc etc


I can't seem to understand what I am doing wrong.

Answer

The HttpResponse class exposes a getEntity method which returns an HttpEntity instance. That provides a mechanism for accessing the reponse content.

You can use EntityUtils to retrieve and consume the content stream for the entity:

private void getData(){
    CloseableHttpClient httpClient = HttpClientBuilder.create().build(); //Use this instead
    CloseableHttpResponse response = null;
    HttpEntity entity = null;

    try {
        HttpPost request = new HttpPost("url goes here");
        StringEntity params = new StringEntity("details={\"process\":\"mobileS\",\"phone\":\"9999999999\"}");
        request.addHeader("content-type", "application/x-www-form-urlencoded");
        request.setEntity(params);

        response = httpClient.execute(request);
        System.out.println(response);

        // handle response here...
        if (successful(response)) {
            entity = response.getEntity();
            String content = EntityUtils.toString(entity);

            System.out.println(content);
        }
    } catch (Exception ex) {
        // handle exception here
    } finally {
        EntityUtils.consumeQuietly(entity);
        if (response != null) response.close();
        if (httpClient != null) httpClient.close();
    }
}

// TODO Customize for your server/interaction
private boolean successful(HttpResponse response) {
    return response != null 
            && response.getStatusLine() != null 
            && response.getStatusLine().getStatusCode() == 200;
}