Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Developing Android Apps at Udacity
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);
}
}
}
@robocide

This comment has been minimized.

Copy link

@robocide robocide commented Sep 27, 2014

supposed to be HttpUrlConnection and not HttpConnection

@rafaell-lycan

This comment has been minimized.

Copy link

@rafaell-lycan rafaell-lycan commented Feb 8, 2015

Hey man, use HttpURLConnection in your connection =)

@Auwalms

This comment has been minimized.

Copy link

@Auwalms Auwalms commented Jul 20, 2016

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
You can’t perform that action at this time.