Instantly share code, notes, and snippets.

Embed
What would you like to do?
2.01 Add the Network Code
// These two need to be declared outside the try/catch
// so that they can be closed in the finally block.
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// Will contain the raw JSON response as a string.
String forecastJsonStr = null;
try {
// Construct the URL for the OpenWeatherMap query
// Possible parameters are avaiable at OWM's forecast API page, at
// http://openweathermap.org/API#forecast
URL url = new URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7");
// Create the request to OpenWeatherMap, and open the connection
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return 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.
return null;
}
forecastJsonStr = buffer.toString();
} catch (IOException e) {
Log.e("PlaceholderFragment", "Error ", e);
// If the code didn't successfully get the weather data, there's no point in attemping
// to parse it.
return null;
} finally{
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("PlaceholderFragment", "Error closing stream", e);
}
}
}
return rootView;
}
}
}
@gildassamuel

This comment has been minimized.

Show comment
Hide comment
@gildassamuel

gildassamuel Feb 28, 2015

thanks for this help

gildassamuel commented Feb 28, 2015

thanks for this help

@DANIELKAKAI

This comment has been minimized.

Show comment
Hide comment
@DANIELKAKAI

DANIELKAKAI commented Mar 14, 2015

thanks alot

@chrisyeung1121

This comment has been minimized.

Show comment
Hide comment
@chrisyeung1121

chrisyeung1121 commented Mar 17, 2015

nice

@sagoyanfisic

This comment has been minimized.

Show comment
Hide comment
@sagoyanfisic

sagoyanfisic commented Mar 19, 2015

nice!

@gpiancastelli

This comment has been minimized.

Show comment
Hide comment
@gpiancastelli

gpiancastelli Mar 21, 2015

Any reason why you are using StringBuffer instead of StringBuilder?

Also, nitpicking: the creation of the buffer could be better placed on line 28 instead of 22, because the buffer is needed only after checking that the input stream is not null.

gpiancastelli commented Mar 21, 2015

Any reason why you are using StringBuffer instead of StringBuilder?

Also, nitpicking: the creation of the buffer could be better placed on line 28 instead of 22, because the buffer is needed only after checking that the input stream is not null.

@bucketzxm

This comment has been minimized.

Show comment
Hide comment
@bucketzxm

bucketzxm commented Mar 25, 2015

thanks !

@rutyroot

This comment has been minimized.

Show comment
Hide comment
@rutyroot

rutyroot Mar 27, 2015

Good, thanks!

rutyroot commented Mar 27, 2015

Good, thanks!

@pravinkerai212

This comment has been minimized.

Show comment
Hide comment
@pravinkerai212

pravinkerai212 Mar 28, 2015

Thanks, this really helps a lot.

pravinkerai212 commented Mar 28, 2015

Thanks, this really helps a lot.

@RaaulCabrera

This comment has been minimized.

Show comment
Hide comment
@RaaulCabrera

RaaulCabrera commented Mar 29, 2015

thanks!!

@lyz1990

This comment has been minimized.

Show comment
Hide comment
@lyz1990

lyz1990 commented Mar 29, 2015

Thanks!!

@pepziman

This comment has been minimized.

Show comment
Hide comment
@pepziman

pepziman Mar 30, 2015

thank you!

pepziman commented Mar 30, 2015

thank you!

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Apr 2, 2015

thanks a lot

ghost commented Apr 2, 2015

thanks a lot

@MagoMtz

This comment has been minimized.

Show comment
Hide comment
@MagoMtz

MagoMtz Apr 3, 2015

why we use the GET method?

MagoMtz commented Apr 3, 2015

why we use the GET method?

@vdugnist

This comment has been minimized.

Show comment
Hide comment
@vdugnist

vdugnist Apr 4, 2015

MagoMtz: because we only want to get data from openweather, not to modify it. You can read about the difference between http methods on wikipedia: http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

vdugnist commented Apr 4, 2015

MagoMtz: because we only want to get data from openweather, not to modify it. You can read about the difference between http methods on wikipedia: http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

@happysahota

This comment has been minimized.

Show comment
Hide comment
@happysahota

happysahota commented Apr 4, 2015

Awesome...

@kolasanichaitanya

This comment has been minimized.

Show comment
Hide comment
@kolasanichaitanya

kolasanichaitanya Apr 5, 2015

In line 34 why to use a stringBuffer ? we can directly append to a string right?

kolasanichaitanya commented Apr 5, 2015

In line 34 why to use a stringBuffer ? we can directly append to a string right?

@npari

This comment has been minimized.

Show comment
Hide comment
@npari

npari Apr 5, 2015

@kolasanichaitanya

The StringBuffer Class allows character representation that are mutable compared to the String class which is immutable. Also, concatenation is faster in StringBuffer. It is mainly a choice to improve performance. The concatenation operation using StringBuffer class and append makes it a cheaper operation.

npari commented Apr 5, 2015

@kolasanichaitanya

The StringBuffer Class allows character representation that are mutable compared to the String class which is immutable. Also, concatenation is faster in StringBuffer. It is mainly a choice to improve performance. The concatenation operation using StringBuffer class and append makes it a cheaper operation.

@ZikasR

This comment has been minimized.

Show comment
Hide comment
@ZikasR

ZikasR commented Apr 11, 2015

thanks

@aindong

This comment has been minimized.

Show comment
Hide comment
@aindong

aindong commented Apr 24, 2015

awesome!

@mike915

This comment has been minimized.

Show comment
Hide comment
@mike915

mike915 Apr 24, 2015

Thanks, it helps a lot!

mike915 commented Apr 24, 2015

Thanks, it helps a lot!

@solisfox

This comment has been minimized.

Show comment
Hide comment
@solisfox

solisfox commented Apr 25, 2015

thanks !

@AliyuAbdullahi

This comment has been minimized.

Show comment
Hide comment
@AliyuAbdullahi

AliyuAbdullahi commented Apr 25, 2015

Thanks

@younesmln

This comment has been minimized.

Show comment
Hide comment
@younesmln

younesmln commented May 2, 2015

good

@refschool

This comment has been minimized.

Show comment
Hide comment
@refschool

refschool May 5, 2015

shouldn't it be return null at the end of the script?

refschool commented May 5, 2015

shouldn't it be return null at the end of the script?

@tunee

This comment has been minimized.

Show comment
Hide comment
@tunee

tunee commented May 13, 2015

cool

@Synix

This comment has been minimized.

Show comment
Hide comment
@Synix

Synix May 16, 2015

thanks!!

Synix commented May 16, 2015

thanks!!

@Haloyum

This comment has been minimized.

Show comment
Hide comment
@Haloyum

Haloyum commented May 20, 2015

Thanks!

@nalamchaitanya

This comment has been minimized.

Show comment
Hide comment
@nalamchaitanya

nalamchaitanya commented May 22, 2015

Helps a lot.

@cmauroie

This comment has been minimized.

Show comment
Hide comment
@cmauroie

cmauroie commented May 25, 2015

Thanks!

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost May 27, 2015

Thanks =)

ghost commented May 27, 2015

Thanks =)

@ewintory

This comment has been minimized.

Show comment
Hide comment
@ewintory

ewintory commented May 31, 2015

Nice :)

@IdiotLeon

This comment has been minimized.

Show comment
Hide comment
@IdiotLeon

IdiotLeon commented Jun 3, 2015

Thanks

@88dave

This comment has been minimized.

Show comment
Hide comment
@88dave

88dave Jun 4, 2015

Good stuff -- useful for lots of situations

88dave commented Jun 4, 2015

Good stuff -- useful for lots of situations

@DeJaVo

This comment has been minimized.

Show comment
Hide comment
@DeJaVo

DeJaVo Jun 6, 2015

Great, thank for this useful code snippet

DeJaVo commented Jun 6, 2015

Great, thank for this useful code snippet

@Curicaveri

This comment has been minimized.

Show comment
Hide comment
@Curicaveri

Curicaveri Jun 10, 2015

Great code!!!

Curicaveri commented Jun 10, 2015

Great code!!!

@dengn

This comment has been minimized.

Show comment
Hide comment
@dengn

dengn Jun 11, 2015

@gpiancastelli
It's very possible that this code is running in multi-thread environment, StringBuffer is synchronized while StringBuilder is not.

dengn commented Jun 11, 2015

@gpiancastelli
It's very possible that this code is running in multi-thread environment, StringBuffer is synchronized while StringBuilder is not.

@theCaffeinatedTanuki

This comment has been minimized.

Show comment
Hide comment
@theCaffeinatedTanuki

theCaffeinatedTanuki commented Jun 12, 2015

Thanks for this!

@mattCheung

This comment has been minimized.

Show comment
Hide comment
@mattCheung

mattCheung Jun 13, 2015

Forgive me if this is a newbie question, but on line 23, when would the inputStream == null but not be triggered by an IOException? Follow up is that shouldn't the fact that nothing was received in the inputStream that wasn't generated from some exception be logged, if nothing else, for info/debug purposes?

mattCheung commented Jun 13, 2015

Forgive me if this is a newbie question, but on line 23, when would the inputStream == null but not be triggered by an IOException? Follow up is that shouldn't the fact that nothing was received in the inputStream that wasn't generated from some exception be logged, if nothing else, for info/debug purposes?

@osirvics

This comment has been minimized.

Show comment
Hide comment
@osirvics

osirvics Jun 17, 2015

Please help, i see on line 37 we are checking if the StringBuffer object "buffer" is equal to zero, but nothing was ever assigned to it, I'm i missing something? (i'm a newbie).

osirvics commented Jun 17, 2015

Please help, i see on line 37 we are checking if the StringBuffer object "buffer" is equal to zero, but nothing was ever assigned to it, I'm i missing something? (i'm a newbie).

@alexandru-calinoiu

This comment has been minimized.

Show comment
Hide comment
@alexandru-calinoiu

alexandru-calinoiu Jun 17, 2015

@osirivics line #34

alexandru-calinoiu commented Jun 17, 2015

@osirivics line #34

@haroldhues

This comment has been minimized.

Show comment
Hide comment
@haroldhues

haroldhues Jun 18, 2015

Not to be a spelling nut but...

s/avaiable/available
s/attemping/attempting

haroldhues commented Jun 18, 2015

Not to be a spelling nut but...

s/avaiable/available
s/attemping/attempting
@gomaa86

This comment has been minimized.

Show comment
Hide comment
@gomaa86

gomaa86 Jun 21, 2015

thank you for help :)

gomaa86 commented Jun 21, 2015

thank you for help :)

@bkkothari

This comment has been minimized.

Show comment
Hide comment
@bkkothari

bkkothari Jun 26, 2015

Thanks for the snippet :)

bkkothari commented Jun 26, 2015

Thanks for the snippet :)

@Mrboutte

This comment has been minimized.

Show comment
Hide comment
@Mrboutte

Mrboutte Jun 28, 2015

Isn't there a better way of doing this with the try with resources block instead? That away the streams are automatically closed

Mrboutte commented Jun 28, 2015

Isn't there a better way of doing this with the try with resources block instead? That away the streams are automatically closed

@ikhfa

This comment has been minimized.

Show comment
Hide comment
@ikhfa

ikhfa Jul 1, 2015

Thanks for the snippet :)

ikhfa commented Jul 1, 2015

Thanks for the snippet :)

@saadeh05

This comment has been minimized.

Show comment
Hide comment
@saadeh05

saadeh05 Jul 1, 2015

I really don't understand most of the code
so is it normal not to understand it all or i'm missing something ?

saadeh05 commented Jul 1, 2015

I really don't understand most of the code
so is it normal not to understand it all or i'm missing something ?

@Anthonyeef

This comment has been minimized.

Show comment
Hide comment
@Anthonyeef

Anthonyeef Jul 2, 2015

@saadeh05
I think it would be nice if you could understand it. But the Udacity course is aiming at the fresh man of Android development, and at this point the understanding of this code doesn't look that important.
So just learn how to use the snippet, and with the more code you read, you will get to understand it.

Anthonyeef commented Jul 2, 2015

@saadeh05
I think it would be nice if you could understand it. But the Udacity course is aiming at the fresh man of Android development, and at this point the understanding of this code doesn't look that important.
So just learn how to use the snippet, and with the more code you read, you will get to understand it.

@TysonMatlhoma

This comment has been minimized.

Show comment
Hide comment
@TysonMatlhoma

TysonMatlhoma Jul 2, 2015

Thanks for the snippet :)

TysonMatlhoma commented Jul 2, 2015

Thanks for the snippet :)

@silentsudo

This comment has been minimized.

Show comment
Hide comment
@silentsudo

silentsudo Jul 5, 2015

Thank you for Gist

silentsudo commented Jul 5, 2015

Thank you for Gist

@y-matsuyama

This comment has been minimized.

Show comment
Hide comment
@y-matsuyama

y-matsuyama commented Jul 11, 2015

thanks

@asinitson

This comment has been minimized.

Show comment
Hide comment
@asinitson

asinitson Jul 11, 2015

@Mrboutte

Isn't there a better way of doing this with the try with resources block instead? That away the streams are automatically closed

I wondered about the same, so I searched a bit and according to what I understand, if you want to use try-with-resources statement (Java SE 7 feature), then you limit yourself to API 19: Android 4.4 (KitKat) and higher.

Source:
java 7 - Does Android support JDK 6 or 7 - Stack Overflow

asinitson commented Jul 11, 2015

@Mrboutte

Isn't there a better way of doing this with the try with resources block instead? That away the streams are automatically closed

I wondered about the same, so I searched a bit and according to what I understand, if you want to use try-with-resources statement (Java SE 7 feature), then you limit yourself to API 19: Android 4.4 (KitKat) and higher.

Source:
java 7 - Does Android support JDK 6 or 7 - Stack Overflow

@Wsakr

This comment has been minimized.

Show comment
Hide comment
@Wsakr

Wsakr Jul 11, 2015

why do we have to call the setRequestMethod("GET") if the default of the HTTPURLConnection is GET

Wsakr commented Jul 11, 2015

why do we have to call the setRequestMethod("GET") if the default of the HTTPURLConnection is GET

@gishiru

This comment has been minimized.

Show comment
Hide comment
@gishiru

gishiru commented Jul 15, 2015

thanks

@njsand

This comment has been minimized.

Show comment
Hide comment
@njsand

njsand Jul 16, 2015

@dengn Use of StringBuffer is wrong here. Even if multiple threads enter this method, they each allocate their own instance (because that's how "new" works), and this instance never "escapes" the method. Thus any internal synchronization that StringBuffer performs is completely wasted. So don't use it. StringBuilder is designed for exactly situations like this.

njsand commented Jul 16, 2015

@dengn Use of StringBuffer is wrong here. Even if multiple threads enter this method, they each allocate their own instance (because that's how "new" works), and this instance never "escapes" the method. Thus any internal synchronization that StringBuffer performs is completely wasted. So don't use it. StringBuilder is designed for exactly situations like this.

@wahooka9

This comment has been minimized.

Show comment
Hide comment
@wahooka9

wahooka9 Jul 21, 2015

@njsand StringBuffer is fine here, and as stated above it has better performance for .append()
To the generation of extra strings on separate threads - I believe that is something you want. The response you get from any requests will be parsed on that thread (and added to that particular stringbuffer). The "threaded" part of networking is that the request posts and a thread that is not the UI thread handles the response and the parsing. This is how they do it in iOS also. To keep it from locking the UI.

wahooka9 commented Jul 21, 2015

@njsand StringBuffer is fine here, and as stated above it has better performance for .append()
To the generation of extra strings on separate threads - I believe that is something you want. The response you get from any requests will be parsed on that thread (and added to that particular stringbuffer). The "threaded" part of networking is that the request posts and a thread that is not the UI thread handles the response and the parsing. This is how they do it in iOS also. To keep it from locking the UI.

@ahmeDatef3112

This comment has been minimized.

Show comment
Hide comment
@ahmeDatef3112

ahmeDatef3112 commented Aug 12, 2015

grazie

@NrTwelve

This comment has been minimized.

Show comment
Hide comment
@NrTwelve

NrTwelve Aug 14, 2015

Thank you!

NrTwelve commented Aug 14, 2015

Thank you!

@du2x

This comment has been minimized.

Show comment
Hide comment
@du2x

du2x Aug 20, 2015

coooool!

du2x commented Aug 20, 2015

coooool!

@jrmineur

This comment has been minimized.

Show comment
Hide comment
@jrmineur

jrmineur Aug 21, 2015

Hi All!

The URL in the code snipped gives me a city in Poland when I hand it to Chrome. I will try this URL for Mountain View - it seems to work in Chrome:

http://api.openweathermap.org/data/2.5/forecast/daily?zip=94043,us&mode=json&units=metric&cnt=7

jrmineur commented Aug 21, 2015

Hi All!

The URL in the code snipped gives me a city in Poland when I hand it to Chrome. I will try this URL for Mountain View - it seems to work in Chrome:

http://api.openweathermap.org/data/2.5/forecast/daily?zip=94043,us&mode=json&units=metric&cnt=7

@jhealy

This comment has been minimized.

Show comment
Hide comment
@jhealy

jhealy Aug 25, 2015

@jrmineur -
on edge in win10 - mountain view
On IE in win10 - montainview
On chrome in win10 - mountainview

Verified the following URL in Jason and xml formats - http://api.openweathermap.org/data/2.5/forecast/daily?zip=94043,us&mode=json&units=metric&cnt=7

Healy in Tampa

jhealy commented Aug 25, 2015

@jrmineur -
on edge in win10 - mountain view
On IE in win10 - montainview
On chrome in win10 - mountainview

Verified the following URL in Jason and xml formats - http://api.openweathermap.org/data/2.5/forecast/daily?zip=94043,us&mode=json&units=metric&cnt=7

Healy in Tampa

@m0nst3rdr1v3n

This comment has been minimized.

Show comment
Hide comment
@m0nst3rdr1v3n

m0nst3rdr1v3n Aug 27, 2015

This is great!

m0nst3rdr1v3n commented Aug 27, 2015

This is great!

@Jorelx

This comment has been minimized.

Show comment
Hide comment
@Jorelx

Jorelx commented Aug 29, 2015

thanks

@taehunzzang

This comment has been minimized.

Show comment
Hide comment
@taehunzzang

taehunzzang Sep 1, 2015

it's good. thanks

taehunzzang commented Sep 1, 2015

it's good. thanks

@vainarouski

This comment has been minimized.

Show comment
Hide comment
@vainarouski

vainarouski Sep 2, 2015

Thanks a lot!

vainarouski commented Sep 2, 2015

Thanks a lot!

@linuxedhorse

This comment has been minimized.

Show comment
Hide comment
@linuxedhorse

linuxedhorse Sep 2, 2015

Java has a try-with-resources, why android hasn't?
But question: why StringBuffer buffer = new StringBuffer(); doesn't under the inputStream != null checking? Here creates the buffer, and if an inputStream is null, there is work for CG, isn't it :)
Thanks anyway!

linuxedhorse commented Sep 2, 2015

Java has a try-with-resources, why android hasn't?
But question: why StringBuffer buffer = new StringBuffer(); doesn't under the inputStream != null checking? Here creates the buffer, and if an inputStream is null, there is work for CG, isn't it :)
Thanks anyway!

@metaruslan

This comment has been minimized.

Show comment
Hide comment
@metaruslan

metaruslan Sep 6, 2015

@dengn,
+1 for @njsand comment. The StringBuffer object is not shared between threads, it's totally "thread-confined", so there is no use in its thread-safety:)

metaruslan commented Sep 6, 2015

@dengn,
+1 for @njsand comment. The StringBuffer object is not shared between threads, it's totally "thread-confined", so there is no use in its thread-safety:)

@yallen011

This comment has been minimized.

Show comment
Hide comment
@yallen011

yallen011 Sep 10, 2015

@jrmineur and @jhealy using the zip parameter doesn't work for all zipcodes. I think getting the correct city every time involves getting the city id but that involves knowing the correct longitude and latitude.

yallen011 commented Sep 10, 2015

@jrmineur and @jhealy using the zip parameter doesn't work for all zipcodes. I think getting the correct city every time involves getting the city id but that involves knowing the correct longitude and latitude.

@maximusg

This comment has been minimized.

Show comment
Hide comment
@maximusg

maximusg Sep 23, 2015

Oh wow that is fairly simple. One newbie question, why do we return the rootView at the end? Also what is with all the extra "}" at the end? Thanks!

maximusg commented Sep 23, 2015

Oh wow that is fairly simple. One newbie question, why do we return the rootView at the end? Also what is with all the extra "}" at the end? Thanks!

@twhoward99

This comment has been minimized.

Show comment
Hide comment
@twhoward99

twhoward99 Sep 24, 2015

Thank you. The previous answer box didn't accept mode=JSON, but the API functioned properly with this parameter. Thanks again.

twhoward99 commented Sep 24, 2015

Thank you. The previous answer box didn't accept mode=JSON, but the API functioned properly with this parameter. Thanks again.

@tuhinsherlock

This comment has been minimized.

Show comment
Hide comment
@tuhinsherlock

tuhinsherlock Oct 1, 2015

Should this be in "MainActivity" class or "MainActivityFragment" class?

tuhinsherlock commented Oct 1, 2015

Should this be in "MainActivity" class or "MainActivityFragment" class?

@zano5

This comment has been minimized.

Show comment
Hide comment
@zano5

zano5 Oct 8, 2015

MainActivity class

zano5 commented Oct 8, 2015

MainActivity class

@timbru31

This comment has been minimized.

Show comment
Hide comment
@timbru31

timbru31 commented Oct 18, 2015

@bahkali

This comment has been minimized.

Show comment
Hide comment
@bahkali

bahkali Oct 19, 2015

thank you. very explanatory

bahkali commented Oct 19, 2015

thank you. very explanatory

@mrrobot97

This comment has been minimized.

Show comment
Hide comment
@mrrobot97

mrrobot97 Oct 25, 2015

Thanks a lot!It's much helpful.

mrrobot97 commented Oct 25, 2015

Thanks a lot!It's much helpful.

@sebcagnon

This comment has been minimized.

Show comment
Hide comment
@sebcagnon

sebcagnon Oct 26, 2015

Why don't we do like this?

static String convertStreamToString(java.io.InputStream is) {
    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}

I am no Java programmer, but this is the first answer for InputStream to String
http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string

I just can't believe an InputStream would require 19 lines, 4 intermediary objects (buffer, reader, line, unnamed InputStreamReader) to convert it to a string.

Is it all for the sake of adding \n in between lines?

Another question, the json output from the query doesn't have lines. So where are we writing all those \n?

sebcagnon commented Oct 26, 2015

Why don't we do like this?

static String convertStreamToString(java.io.InputStream is) {
    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}

I am no Java programmer, but this is the first answer for InputStream to String
http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string

I just can't believe an InputStream would require 19 lines, 4 intermediary objects (buffer, reader, line, unnamed InputStreamReader) to convert it to a string.

Is it all for the sake of adding \n in between lines?

Another question, the json output from the query doesn't have lines. So where are we writing all those \n?

@marianosalvetti

This comment has been minimized.

Show comment
Hide comment
@marianosalvetti

marianosalvetti Nov 3, 2015

Thanks a lot, it's helpful .

marianosalvetti commented Nov 3, 2015

Thanks a lot, it's helpful .

@ganeshachary

This comment has been minimized.

Show comment
Hide comment
@ganeshachary

ganeshachary commented Nov 8, 2015

Thanks

@Rolos

This comment has been minimized.

Show comment
Hide comment
@Rolos

Rolos Nov 22, 2015

I'm here too!

Rolos commented Nov 22, 2015

I'm here too!

@dandv

This comment has been minimized.

Show comment
Hide comment
@dandv

dandv Nov 25, 2015

Like @sebcagnon above, this low-level Java code makes my head hurt. The equivalent JavaScript code for the Meteor framework (which generates web and mobile apps from the same code base) is far, far simpler:

HTTP.get("http://api.openweathermap.org/data/...", function (error, result) {
    if (error) {
        ...
    } else {
        let weatherData = result.data;  // JSON automatically parsed
    }
});

No need to get more complicated than what you actually want to do - get a JSON string via HTTP. Given that this course is aimed at beginners, it might have made more sense to use a high-level HTTP library such as Volley.

PS: no need to say "Thanks"; GitHub doesn't send notifications of activity on gists.

dandv commented Nov 25, 2015

Like @sebcagnon above, this low-level Java code makes my head hurt. The equivalent JavaScript code for the Meteor framework (which generates web and mobile apps from the same code base) is far, far simpler:

HTTP.get("http://api.openweathermap.org/data/...", function (error, result) {
    if (error) {
        ...
    } else {
        let weatherData = result.data;  // JSON automatically parsed
    }
});

No need to get more complicated than what you actually want to do - get a JSON string via HTTP. Given that this course is aimed at beginners, it might have made more sense to use a high-level HTTP library such as Volley.

PS: no need to say "Thanks"; GitHub doesn't send notifications of activity on gists.

@deserthero

This comment has been minimized.

Show comment
Hide comment
@deserthero

deserthero Nov 29, 2015

Amazing, thank you

deserthero commented Nov 29, 2015

Amazing, thank you

@nareshcode

This comment has been minimized.

Show comment
Hide comment
@nareshcode

nareshcode Nov 30, 2015

i didnt get what is rootview the return type variable.It is a string right

nareshcode commented Nov 30, 2015

i didnt get what is rootview the return type variable.It is a string right

@SeifMostafa

This comment has been minimized.

Show comment
Hide comment
@SeifMostafa

SeifMostafa Dec 9, 2015

don't forget to make comment in the url as the website needs id for now

SeifMostafa commented Dec 9, 2015

don't forget to make comment in the url as the website needs id for now

@anandbibek

This comment has been minimized.

Show comment
Hide comment
@anandbibek

anandbibek Dec 12, 2015

I'm not exactly sure, but the correct url seems to be http://api.openweathermap.org/data/2.5/forecast/daily?id=5375480&cnt=7&appid=xxxxxxxxxxxxxxxxxxxxxxxx

Can't figure out what is q parameter being used, which is not mentioned in current API. And the present url gives a city in Poland. Basically because we have to use id now, and zip code is deprecated I guess.

anandbibek commented Dec 12, 2015

I'm not exactly sure, but the correct url seems to be http://api.openweathermap.org/data/2.5/forecast/daily?id=5375480&cnt=7&appid=xxxxxxxxxxxxxxxxxxxxxxxx

Can't figure out what is q parameter being used, which is not mentioned in current API. And the present url gives a city in Poland. Basically because we have to use id now, and zip code is deprecated I guess.

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Dec 15, 2015

Consider this URL. When requesting postal codes use the 'zip' parameter. Also the new format for weather apis require an appid.

http://api.openweathermap.org/data/2.5/forecast?zip=94043&metric&cnt=7&appid=2de143494c0b295cca9337e1e96b00e0

The codes presented in this course are not static. As the days go by, newer libraries are created and some codes become deprecated. It's up to you, the programmer, to debug the code that doesn't work.

ghost commented Dec 15, 2015

Consider this URL. When requesting postal codes use the 'zip' parameter. Also the new format for weather apis require an appid.

http://api.openweathermap.org/data/2.5/forecast?zip=94043&metric&cnt=7&appid=2de143494c0b295cca9337e1e96b00e0

The codes presented in this course are not static. As the days go by, newer libraries are created and some codes become deprecated. It's up to you, the programmer, to debug the code that doesn't work.

@khanhnv3007

This comment has been minimized.

Show comment
Hide comment
@khanhnv3007

khanhnv3007 commented Dec 17, 2015

nice 💃

@williamboxhall

This comment has been minimized.

Show comment
Hide comment
@williamboxhall

williamboxhall commented Jan 15, 2016

ace!

@b03902043

This comment has been minimized.

Show comment
Hide comment
@b03902043

b03902043 commented Jan 24, 2016

great!

@ambitionwang277

This comment has been minimized.

Show comment
Hide comment
@ambitionwang277

ambitionwang277 commented Jan 26, 2016

Thanks a lot.

@marciliooliveira

This comment has been minimized.

Show comment
Hide comment
@marciliooliveira

marciliooliveira Jan 29, 2016

Guys, someone can help me?

I've gotten this error when I put my code on fragment, It's about async (I saw in StackOverflow) threads or some like that?

01-29 00:56:10.194 21852-21852/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.sunshine.app, PID: 21852
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.sunshine.app/com.example.android.sunshine.app.MainActivity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)

Tks.

marciliooliveira commented Jan 29, 2016

Guys, someone can help me?

I've gotten this error when I put my code on fragment, It's about async (I saw in StackOverflow) threads or some like that?

01-29 00:56:10.194 21852-21852/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.sunshine.app, PID: 21852
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.sunshine.app/com.example.android.sunshine.app.MainActivity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)

Tks.

@JustinNam

This comment has been minimized.

Show comment
Hide comment
@JustinNam

JustinNam Jan 30, 2016

why in my code error buffer not initialization ??

JustinNam commented Jan 30, 2016

why in my code error buffer not initialization ??

@rishsethia

This comment has been minimized.

Show comment
Hide comment
@rishsethia

rishsethia Feb 1, 2016

Why do we need a inputstream when we have a bufferdreader? Actually I am confused about this . Could someone please help?

rishsethia commented Feb 1, 2016

Why do we need a inputstream when we have a bufferdreader? Actually I am confused about this . Could someone please help?

@SiddharthPant

This comment has been minimized.

Show comment
Hide comment
@SiddharthPant

SiddharthPant Feb 1, 2016

In my competitive coding escapades I have used more complex form of input reading system which involved StringTokenizer etc. So this is not new to me. Why are we using these two? Well its because InputStreamReader takes byte stream and converts into one character at a time from input while BufferedReader then stores that and keeps buffering till a full line has been read. When this has been done we then just need to read our buffered string in one go. Without BufferedReader we will have to use a loop that stores everything in a char array character by character not a very efficient proposition. As for not using Scanner class I can't say if its the reason but in my experience Scanner class is terribly slow.

SiddharthPant commented Feb 1, 2016

In my competitive coding escapades I have used more complex form of input reading system which involved StringTokenizer etc. So this is not new to me. Why are we using these two? Well its because InputStreamReader takes byte stream and converts into one character at a time from input while BufferedReader then stores that and keeps buffering till a full line has been read. When this has been done we then just need to read our buffered string in one go. Without BufferedReader we will have to use a loop that stores everything in a char array character by character not a very efficient proposition. As for not using Scanner class I can't say if its the reason but in my experience Scanner class is terribly slow.

@fzls

This comment has been minimized.

Show comment
Hide comment
@fzls

fzls Feb 2, 2016

thanks a lot!

fzls commented Feb 2, 2016

thanks a lot!

@abhi007tyagi

This comment has been minimized.

Show comment
Hide comment
@abhi007tyagi

abhi007tyagi Feb 2, 2016

@dengn @njsand @wahooka9
check below link. Looks like String Builder would be better choice.

abhi007tyagi commented Feb 2, 2016

@dengn @njsand @wahooka9
check below link. Looks like String Builder would be better choice.

@AIRAT1

This comment has been minimized.

Show comment
Hide comment
@AIRAT1

AIRAT1 Feb 6, 2016

Herzlichen Dank!

AIRAT1 commented Feb 6, 2016

Herzlichen Dank!

@abhinandankothari

This comment has been minimized.

Show comment
Hide comment
@abhinandankothari

abhinandankothari Feb 6, 2016

@abhi007tyagi Thanks for the link 👍

abhinandankothari commented Feb 6, 2016

@abhi007tyagi Thanks for the link 👍

@TasneemS

This comment has been minimized.

Show comment
Hide comment
@TasneemS

TasneemS Feb 8, 2016

I think this code should be in the MainActivityFragment class in newer versions.

TasneemS commented Feb 8, 2016

I think this code should be in the MainActivityFragment class in newer versions.

@Ashish680

This comment has been minimized.

Show comment
Hide comment
@Ashish680

Ashish680 Feb 11, 2016

http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7

this given url in the code above is not working it showing a error page

Ashish680 commented Feb 11, 2016

http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7

this given url in the code above is not working it showing a error page

@pitlv2109

This comment has been minimized.

Show comment
Hide comment
@pitlv2109

pitlv2109 Feb 11, 2016

@Ashish680
Now that the OpenWeatherMap requires API key to make requests (http://openweathermap.org/faq#error401). It should be http://api.openweathermap.org/data/2.5/forecast/daily?zip=94043&mode=json&units=metric&cnt=7&APPID=put-your-id-here. (You can always paste it into your web browser to check the result).

pitlv2109 commented Feb 11, 2016

@Ashish680
Now that the OpenWeatherMap requires API key to make requests (http://openweathermap.org/faq#error401). It should be http://api.openweathermap.org/data/2.5/forecast/daily?zip=94043&mode=json&units=metric&cnt=7&APPID=put-your-id-here. (You can always paste it into your web browser to check the result).

@mousaab

This comment has been minimized.

Show comment
Hide comment
@mousaab

mousaab Feb 14, 2016

It is true that the OpenWeatherMap requires API key
put any one can use this key:
appid=44db6a862fba0b067b1930da0d769e98
so your URL should be like this ;
http://api.openweathermap.org/data/2.5/find?lat=57&lon=-2.15&cnt=3&appid=44db6a862fba0b067b1930da0d769e98

mousaab commented Feb 14, 2016

It is true that the OpenWeatherMap requires API key
put any one can use this key:
appid=44db6a862fba0b067b1930da0d769e98
so your URL should be like this ;
http://api.openweathermap.org/data/2.5/find?lat=57&lon=-2.15&cnt=3&appid=44db6a862fba0b067b1930da0d769e98

@denkomanceski

This comment has been minimized.

Show comment
Hide comment
@denkomanceski

denkomanceski Feb 22, 2016

I cant believe that it takes 60 lines of code to make one GET request. I come from web background. We need 1 line there :(

denkomanceski commented Feb 22, 2016

I cant believe that it takes 60 lines of code to make one GET request. I come from web background. We need 1 line there :(

@galagade

This comment has been minimized.

Show comment
Hide comment
@galagade

galagade Feb 23, 2016

beautiful code

galagade commented Feb 23, 2016

beautiful code

@bkmagnetron

This comment has been minimized.

Show comment
Hide comment
@bkmagnetron

bkmagnetron Feb 23, 2016

@dandv @denkomanceski 👍 same feeling And I think more annoyingly, you don't see all of the try-catch blocks here.

Here is a simpler version.
how to send HttpRequest and get Json response in android?

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget= new HttpGet(new URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7"));
String forecastJsonStr;

HttpResponse response = httpclient.execute(httpget);

if(response.getStatusLine().getStatusCode()==200){
   forecastJsonStr = EntityUtils.toString(response.getEntity());
} else {
   Log.i("Server response", "Failed to get server response" );
}

bkmagnetron commented Feb 23, 2016

@dandv @denkomanceski 👍 same feeling And I think more annoyingly, you don't see all of the try-catch blocks here.

Here is a simpler version.
how to send HttpRequest and get Json response in android?

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget= new HttpGet(new URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7"));
String forecastJsonStr;

HttpResponse response = httpclient.execute(httpget);

if(response.getStatusLine().getStatusCode()==200){
   forecastJsonStr = EntityUtils.toString(response.getEntity());
} else {
   Log.i("Server response", "Failed to get server response" );
}
@MostafaAnter

This comment has been minimized.

Show comment
Hide comment
@MostafaAnter

MostafaAnter Feb 27, 2016

i recommend using volley is the best solution with few lines 👍
volley, Easy fast Networking library for android

MostafaAnter commented Feb 27, 2016

i recommend using volley is the best solution with few lines 👍
volley, Easy fast Networking library for android

@skielosky

This comment has been minimized.

Show comment
Hide comment
@skielosky

skielosky commented Feb 27, 2016

Thanks !

@bkmagnetron

This comment has been minimized.

Show comment
Hide comment
@bkmagnetron

bkmagnetron Mar 2, 2016

This is my understanding of this code snippet,

Task
We have to send a HTTP request to an URL and GET some data.

Solving

  • Create a new variable for the URL
    new URL(...)
  • Use the URL to create and open HttpURLConnection
    openConnection()
  • Now send a GET request
    setRequestMethod("GET")
  • If we get response for our request
    inputStream != null
    buffer the data and store it as a String.
    new BufferedReader()
  • And "finally" close the HttpURLConnection
    disconnect()

Simply as open, request, response, close.

bkmagnetron commented Mar 2, 2016

This is my understanding of this code snippet,

Task
We have to send a HTTP request to an URL and GET some data.

Solving

  • Create a new variable for the URL
    new URL(...)
  • Use the URL to create and open HttpURLConnection
    openConnection()
  • Now send a GET request
    setRequestMethod("GET")
  • If we get response for our request
    inputStream != null
    buffer the data and store it as a String.
    new BufferedReader()
  • And "finally" close the HttpURLConnection
    disconnect()

Simply as open, request, response, close.

@Ashwin-Kapes

This comment has been minimized.

Show comment
Hide comment
@Ashwin-Kapes

Ashwin-Kapes Mar 10, 2016

Instead of (buffer.length() == 0) can we write (buffer == null). Please explain.

Ashwin-Kapes commented Mar 10, 2016

Instead of (buffer.length() == 0) can we write (buffer == null). Please explain.

@augustocarmo

This comment has been minimized.

Show comment
Hide comment
@augustocarmo

augustocarmo Mar 15, 2016

Thank you very much!!!

augustocarmo commented Mar 15, 2016

Thank you very much!!!

@wilik16

This comment has been minimized.

Show comment
Hide comment
@wilik16

wilik16 Mar 16, 2016

@Ashwin-Kapes you can't, because the buffer is instantiated already = new StringBuffer(); thus not null.

wilik16 commented Mar 16, 2016

@Ashwin-Kapes you can't, because the buffer is instantiated already = new StringBuffer(); thus not null.

@saberelharti

This comment has been minimized.

Show comment
Hide comment
@saberelharti

saberelharti commented Mar 16, 2016

thank u

@AbdouElnemr

This comment has been minimized.

Show comment
Hide comment
@AbdouElnemr

AbdouElnemr commented Mar 16, 2016

Thank You

@kyodgorbek

This comment has been minimized.

Show comment
Hide comment
@kyodgorbek

kyodgorbek Mar 17, 2016

I am getting error following error

Executing tasks: [:app:assembleDebug]

Configuration on demand is an incubating feature.
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAnimatedVectorDrawable2321Library UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72321Library UP-TO-DATE
:app:prepareComAndroidSupportDesign2321Library UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72321Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42321Library UP-TO-DATE
:app:prepareComAndroidSupportSupportVectorDrawable2321Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAppindexing810Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesBasement810Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:compileDebugJavaWithJavac
/home/yodgorbek/AndroidStudioProjects/Sunshine/app/src/main/java/com/example/android/sunshine/MainActivity.java:117: error: non-static method getInputStream() cannot be referenced from a static context
InputStream inputStream = URLConnection.getInputStream();
^
Note: /home/yodgorbek/AndroidStudioProjects/Sunshine/app/src/main/java/com/example/android/sunshine/MainActivity.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':app:compileDebugJavaWithJavac'.

    Compilation failed; see the compiler error output for details.

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 1.303 secs

kyodgorbek commented Mar 17, 2016

I am getting error following error

Executing tasks: [:app:assembleDebug]

Configuration on demand is an incubating feature.
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAnimatedVectorDrawable2321Library UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72321Library UP-TO-DATE
:app:prepareComAndroidSupportDesign2321Library UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72321Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42321Library UP-TO-DATE
:app:prepareComAndroidSupportSupportVectorDrawable2321Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAppindexing810Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesBasement810Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:compileDebugJavaWithJavac
/home/yodgorbek/AndroidStudioProjects/Sunshine/app/src/main/java/com/example/android/sunshine/MainActivity.java:117: error: non-static method getInputStream() cannot be referenced from a static context
InputStream inputStream = URLConnection.getInputStream();
^
Note: /home/yodgorbek/AndroidStudioProjects/Sunshine/app/src/main/java/com/example/android/sunshine/MainActivity.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':app:compileDebugJavaWithJavac'.

    Compilation failed; see the compiler error output for details.

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 1.303 secs

@alexisforce

This comment has been minimized.

Show comment
Hide comment
@alexisforce

alexisforce Mar 22, 2016

Google + Udacity = Best Combination ever.... Thanks for all, Very intuitive way to teach

alexisforce commented Mar 22, 2016

Google + Udacity = Best Combination ever.... Thanks for all, Very intuitive way to teach

@KosratDAhmad

This comment has been minimized.

Show comment
Hide comment
@KosratDAhmad

KosratDAhmad Mar 22, 2016

Thank you, That is great

KosratDAhmad commented Mar 22, 2016

Thank you, That is great

@JLundho

This comment has been minimized.

Show comment
Hide comment
@JLundho

JLundho Mar 27, 2016

To mix it up and simplify the networking tasks, I used OkHttp to perform the network request (which also reduced the boilerplate), but this resulted in an exception saying that I was attempting to perform network operations on the main thread. Did this always create an exception?
If you stumble onto the same problem, use AsyncTask and remember to request the appropriate permissions.

JLundho commented Mar 27, 2016

To mix it up and simplify the networking tasks, I used OkHttp to perform the network request (which also reduced the boilerplate), but this resulted in an exception saying that I was attempting to perform network operations on the main thread. Did this always create an exception?
If you stumble onto the same problem, use AsyncTask and remember to request the appropriate permissions.

@mahasagar

This comment has been minimized.

Show comment
Hide comment
@mahasagar

mahasagar commented Apr 4, 2016

awesome

@Danihelsan

This comment has been minimized.

Show comment
Hide comment
@Danihelsan

Danihelsan commented Apr 5, 2016

Thanks

@Nicofisi

This comment has been minimized.

Show comment
Hide comment
@Nicofisi

Nicofisi commented Apr 9, 2016

Great ^^

@PrafullaKumarSahu

This comment has been minimized.

Show comment
Hide comment
@PrafullaKumarSahu

PrafullaKumarSahu Apr 12, 2016

@Nicofisi and @Danihelsan if possible please share your snippet I am getting error

04-12 20:31:01.652 5740-5740/com.android.serverwarrior.sunshine.app E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.serverwarrior.sunshine.app/com.android.serverwarrior.sunshine.app.MainActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
                                                                                          at android.app.ActivityThread.access$700(ActivityThread.java:157)
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                          at android.os.Looper.loop(Looper.java:176)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5317)
                                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                          at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
                                                                                          at dalvik.system.NativeStart.main(Native Method)
                                                                                       Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
                                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
                                                                                          at android.view.LayoutInflater.parseInclude(LayoutInflater.java:818)
                                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
                                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
                                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
                                                                                          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                                                          at com.android.serverwarrior.sunshine.app.MainActivity.onCreate(MainActivity.java:17)
                                                                                          at android.app.Activity.performCreate(Activity.java:5326)
                                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309) 
                                                                                          at android.app.ActivityThread.access$700(ActivityThread.java:157) 
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289) 
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                          at android.os.Looper.loop(Looper.java:176) 
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5317) 
                                                                                          at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                          at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
                                                                                          at dalvik.system.NativeStart.main(Native Method) 
                                                                                       Caused by: android.os.NetworkOnMainThreadException
                                                                                          at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
                                                                                          at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
                                                                                          at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
                                                                                          at java.net.InetAddress.getAllByName(InetAddress.java:214)
                                                                                          at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
                                                                                          at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
                                                                                          at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
                                                                                          at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
                                                                                          at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
                                                                                          at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
                                                                                          at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
                                                                                          at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
                                                                                          at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
                                                                                          at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
                                                                                          at com.android.serverwarrior.sunshine.app.MainActivityFragment.onCreateView(MainActivityFragment.java:68)
                                                                                          at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
                                                                                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
                                                                                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
                                                                                          at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
                                                                                          at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
                                                                                          at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
                                                                                          at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
                                                                                          at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
                                                                                          at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80)
                                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)

PrafullaKumarSahu commented Apr 12, 2016

@Nicofisi and @Danihelsan if possible please share your snippet I am getting error

04-12 20:31:01.652 5740-5740/com.android.serverwarrior.sunshine.app E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.serverwarrior.sunshine.app/com.android.serverwarrior.sunshine.app.MainActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
                                                                                          at android.app.ActivityThread.access$700(ActivityThread.java:157)
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                          at android.os.Looper.loop(Looper.java:176)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5317)
                                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                          at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
                                                                                          at dalvik.system.NativeStart.main(Native Method)
                                                                                       Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
                                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
                                                                                          at android.view.LayoutInflater.parseInclude(LayoutInflater.java:818)
                                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
                                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
                                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
                                                                                          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                                                          at com.android.serverwarrior.sunshine.app.MainActivity.onCreate(MainActivity.java:17)
                                                                                          at android.app.Activity.performCreate(Activity.java:5326)
                                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309) 
                                                                                          at android.app.ActivityThread.access$700(ActivityThread.java:157) 
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289) 
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                          at android.os.Looper.loop(Looper.java:176) 
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5317) 
                                                                                          at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                          at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
                                                                                          at dalvik.system.NativeStart.main(Native Method) 
                                                                                       Caused by: android.os.NetworkOnMainThreadException
                                                                                          at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
                                                                                          at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
                                                                                          at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
                                                                                          at java.net.InetAddress.getAllByName(InetAddress.java:214)
                                                                                          at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
                                                                                          at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
                                                                                          at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
                                                                                          at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
                                                                                          at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
                                                                                          at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
                                                                                          at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
                                                                                          at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
                                                                                          at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
                                                                                          at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
                                                                                          at com.android.serverwarrior.sunshine.app.MainActivityFragment.onCreateView(MainActivityFragment.java:68)
                                                                                          at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
                                                                                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
                                                                                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
                                                                                          at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
                                                                                          at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
                                                                                          at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
                                                                                          at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
                                                                                          at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
                                                                                          at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80)
                                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
@ronengi

This comment has been minimized.

Show comment
Hide comment
@ronengi

ronengi commented Apr 14, 2016

Thanks

@andredelgado

This comment has been minimized.

Show comment
Hide comment
@andredelgado

andredelgado commented Apr 15, 2016

thanks

@miriban

This comment has been minimized.

Show comment
Hide comment
@miriban

miriban commented Apr 16, 2016

Thanks

@marlonlom

This comment has been minimized.

Show comment
Hide comment
@marlonlom

marlonlom Apr 20, 2016

@PrafullaKumarSahu ... i think is related to yout fragment initialization, besides, have you reviewed the http response of the query?
i just copied that snippet and it works :)

marlonlom commented Apr 20, 2016

@PrafullaKumarSahu ... i think is related to yout fragment initialization, besides, have you reviewed the http response of the query?
i just copied that snippet and it works :)

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost May 7, 2016

Thanks!

ghost commented May 7, 2016

Thanks!

@Tacfarinas

This comment has been minimized.

Show comment
Hide comment
@Tacfarinas

Tacfarinas commented May 17, 2016

great

@progdog

This comment has been minimized.

Show comment
Hide comment
@progdog

progdog May 23, 2016

Thank you very much!

progdog commented May 23, 2016

Thank you very much!

@mur7ay

This comment has been minimized.

Show comment
Hide comment
@mur7ay

mur7ay May 23, 2016

@PrafullaKumarSahu Did you figure it out or do you still need help?

mur7ay commented May 23, 2016

@PrafullaKumarSahu Did you figure it out or do you still need help?

@bangonkali

This comment has been minimized.

Show comment
Hide comment
@bangonkali

bangonkali commented May 27, 2016

Nice.

@Hossein-Ghanaati

This comment has been minimized.

Show comment
Hide comment
@Hossein-Ghanaati