Olá!
Eu tenho uma classe que faz um POST, e eu consegui enviar mas não entendi como pegar a resposta.
Eu envio um JSON e preciso receber um JSON como resposta, aí eu realmente tô perdida.
Tô usando o GSON e o Apache HTTP 4.5, by the way…
Code:
class SenderPost {
private HttpResponse response;
private String gaResponse;
Integer postLogin(String login, String password) throws UnsupportedEncodingException {
String send = "{\"login\" : \""+login+"\" , \"password\" : \""+password+"\"}";
HttpClient client = HttpClientBuilder.create().build();
CloseableHttpClient clientClose = HttpClients.createDefault();
HttpPost post = new HttpPost("url_do_post");
StringEntity entity = new StringEntity(send);
post.setHeader("Accept", "application/json");
post.setHeader("Content-Type","application/json");
post.setHeader("Authorization","Bearer token grandão");
post.setEntity(entity);
try {
response = client.execute(post);
clientClose.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null,"Não foi possível se conectar aos servidores");
}
post.releaseConnection();
return response.getStatusLine().getStatusCode();
}
}