Skip to content

Instantly share code, notes, and snippets.

@steppat
Created June 23, 2015 21:43
Show Gist options
  • Save steppat/701c9084ed96a7a0b017 to your computer and use it in GitHub Desktop.
Save steppat/701c9084ed96a7a0b017 to your computer and use it in GitHub Desktop.
Android HttpURLConnection
try {
URL url = new URL("http://www.caelum.com.br/mobile");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-type", "application/json");
//coloca o json no corpo do POST
connection.setDoOutput(true);
PrintStream printStream = new PrintStream(connection.getOutputStream());
printStream.println(json);
//envia para o servidor
connection.connect();
//trata resposta (que no nosso caso só tem uma linha)
String jsonDeResposta = new Scanner(connection.getInputStream()).next();
return jsonDeResposta;
} catch (Exception e) {
throw new RuntimeException(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment