0

I'm using XmlPullParser to parse the weather information from the API. My application shows the weather information once the user entered his city (input). If I enter enter "London" in the text box, my app goes to this and fetches the temperature, humidity etc. from the xml (at this point everything works as expected). But when I enter "Londonsd" my app hangs up.

Message I'm getting from the server when I enter incorrect city is "{"message":"Error: Not found city","cod":"404"}".

John Slegers
  • 45,213
  • 22
  • 199
  • 169
DaX
  • 7
  • 5
  • simply parse the response from the server and apply conditions to give message to user. In above case, if you are getting above message, check value pair for the key "message". If the value happens to be "Error"Not found city", use TextView or something in your layout to show no city found. – Sujit Devkar May 13 '15 at 04:38
  • 1
    Thanks @Sujit I used the same idea as yours. – DaX May 13 '15 at 15:02

1 Answers1

1

You are getting two types of data responses in case of success get xml and in case of failure getting json make them identical first. I will suggest use json by changing mode=json http://api.openweathermap.org/data/2.5/weather?q=London&mode=json&units=metric.

After that you can put a check on cod if there is 404 then city is invalid.

or

You can try response.contains("\"cod\":\"404\""); or response.contains("Not found city"); for checking error. but not ideal solution.

Ahmad Nawaz
  • 1,129
  • 7
  • 26
  • thank you for your response @Ahmad Nawaz. I don't want to change it to json, because I have to rewrite the code again. – DaX May 13 '15 at 04:39
  • Then you can try `response.contains("\"cod\":\"404\"");` or `response.contains("Not found city");` for checking error. but not ideal solution. – Ahmad Nawaz May 13 '15 at 04:43