0

I'm working with online services, better said, learning how to work with them. I have to request weather data from Yahoo's weather API, and read it in JSon. But it seems that Yahoo doesn't provide now the data in Json, so I have to get it in XML.

I have the code done to get it in JSon, but I'm not sure on how to do it with XML.

This is the request code:

URL url = null;
HttpURLConnection connection = null;

url = new URL(WEATHER_URL + code);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");

InputStream is = connection.getInputStream();

//Parse response here
WeatherInfo info = readWeatherInfo(is);
return info;

This is how I get it in JSon:

private WeatherInfo readWeatherInfo(InputStream is){
        if (is == null)
            return null;

        WeatherInfo info = new WeatherInfo();
        JsonReader reader = null;

        try {

            reader = new JsonReader(new InputStreamReader(is));
            reader.beginObject();

            while (reader.hasNext()){        
                if (isCancelled()) break;

                String name = reader.nextName();
                if (name.equals(LOCATION_NAME)){            //Location

                    reader.beginObject();
                    while (reader.hasNext()){
                        String name2 = reader.nextName();
                        if (name2.equals(CITY_NAME)){
                            info.city = reader.nextString();
                        } else reader.skipValue();
                    }
                    reader.endObject();

                } else if (){
                    //...
                }     

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    reader.close();
                } catch (IOException e) {
                        e.printStackTrace();
                }
            }
            return info;
        }
    }
}

And this is the way I've started to do it in XML but not sure about it:

try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = factory.newPullParser();
            parser.setInput(new InputStreamReader(is));

            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT){

                switch (eventType) {
                    case XmlPullParser.START_TAG:
                        while (parser.next()) {
                            String name = parser.getName();
                            if (name.equals(LOCATION_NAME)){
                                String name2 = parser.getName();
                                if (name2.equals(CITY_NAME)) {
                                    info.city = parser.getText();
                                }
                            }

                            else if (name.equals(CONDITION_NAME)){
                                //...
                            }

UPDATE - XML file link:

http://weather.yahooapis.com/forecastrss?w=766273&u=c

masmic
  • 3,526
  • 11
  • 52
  • 105
  • A sample of the XML would be helpful. – s1m3n Jan 27 '14 at 11:03
  • @s1m3n What do you mean? I haven't got more code, this is all I've got. As I said, I have done it to get it with JSon, but no idea on how to get it in XML. – masmic Jan 27 '14 at 11:06
  • You are parsing XML but you do not provide a sample of the XML structure, so I have not idea if your XML parsing code is on track. I guess I could look up the Yahoo API, but this is your question. – s1m3n Jan 27 '14 at 11:32
  • @s1m3n Yes, sorry, your're right. I've updated the post with the link to the XML file – masmic Jan 28 '14 at 07:57

1 Answers1

2

This is an excellent tutorial for XML parsing on Android.

If you want, download the source code.

You will find a generic XML parser, which you can use universally.

Swayam
  • 16,294
  • 14
  • 64
  • 102
  • Nice one, but I have to do it using SAX instead of DOM. Not sure if there is relevant diferences between them or I can still use this example. – masmic Jan 27 '14 at 11:27
  • I would be wrong if I said that SAX and DOM are same. You can look up on the differences here : http://stackoverflow.com/questions/6828703/what-is-the-difference-between-sax-and-dom. But in your scenario, you are better off with DOM anyway. So, this code would work like a charm. I use it in all my projects. – Swayam Jan 27 '14 at 11:30