Search code examples
javajsongsongoogle-cloud-storagerestlet

How to get the response data from cloud to string


I want to get the response to a string variable from the data from the cloud.

ClientResource cr = new ClientResource("http://localhost:8888/users");
cr.setRequestEntityBuffering(true);
try {
    try {
        cr.get(MediaType.APPLICATION_JSON).write(System.out);
    } catch (IOException e) {

        e.printStackTrace();
    }
} catch (ResourceException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

I have response as JSON in the console and I want to convert it to string , Is the GSON library would be helpful? I haven't used it yet .What modifications should I need to do in my codes? Can anybody help me here.


Solution

  • In fact, Restlet receives the response payload as String and you can directly have access to this, as described below:

    ClientResource cr = new ClientResource("http://localhost:8888/users");
    cr.setRequestEntityBuffering(true);   
    
    Representation representation = cr.get(MediaType.APPLICATION_JSON);
    String jsonContentAsString = representation.getText();
    

    Hope it helps you, Thierry