-
-
Save realdm/e5f17397db977bfea4da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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