Skip to content

Instantly share code, notes, and snippets.

@realdm
Created March 12, 2015 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save realdm/e5f17397db977bfea4da to your computer and use it in GitHub Desktop.
Save realdm/e5f17397db977bfea4da to your computer and use it in GitHub Desktop.
public String carregarDadosDeTemperatura(String stringUrl)
{
HttpURLConnection conexao = null;
BufferedReader reader=null;
String jsonTemperatura = null;
InputStream inputStream=null;
URL url = null;
try {
url = new URL(stringUrl);
conexao = (HttpURLConnection) url.openConnection();
conexao.setRequestMethod("GET");
conexao.setReadTimeout(10000);
inputStream = conexao.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
jsonTemperatura = null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
jsonTemperatura = null;
}
jsonTemperatura = buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
}
catch(IOException e)
{
}
finally {
if(inputStream!=null)
{
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return jsonTemperatura;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment