Last active
July 20, 2016 11:15
-
-
Save KamilLelonek/cec2a775a439ce2ebc4e to your computer and use it in GitHub Desktop.
Developing Android Apps at Udacity
This file contains hidden or 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
Httpconnection connection = null; | |
BufferedReader reader = null; | |
String forecastJsonStr = null; | |
try { | |
URL url = new URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7"); | |
connection = (Httpconnection) url.openConnection(); | |
connection.setRequestMethod("GET"); | |
connection.connect(); | |
InputStream inputStream = connection.getInputStream(); | |
StringBuilder builder = new StringBuilder(); | |
reader = new BufferedReader(new InputStreamReader(inputStream)); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
builder.append(line + "\n"); | |
} | |
forecastJsonStr = builder.toString(); | |
} catch (IOException e) { | |
Log.e("PlaceholderFragment", "Error ", e); | |
} finally { | |
if (connection != null) { | |
connection.disconnect(); | |
} | |
if (reader != null) { | |
try { | |
reader.close(); | |
} catch (final IOException e) { | |
Log.e("PlaceholderFragment", "Error closing stream", e); | |
} | |
} | |
} |
Hey man, use HttpURLConnection in your connection =)
cool, why are you more comfortable with StringBuilder than StringBuffer?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
supposed to be HttpUrlConnection and not HttpConnection