androidweather-apiandroid-xmlpullparseropenweathermap

How to tell if the city is incorrect in Android openweatherMap API?


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"}".


Solution

  • 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.