I am trying to parse openweathermap api http://api.openweathermap.org/data/2.5/weather?q=London&mode=xml
. I am using KXmlParser via XmlPullParserFactory.newPullParser() but the XMLPullParser.getName() is always returning null. It might be some silly mistake but it seems that I might need your help to see it.
Here's the code to read the api via HTTPURLConnection
.
URL url = new URL(urlString);
URLConnection urlConnection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) urlConnection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
if(httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK){
inputStream = httpConnection.getInputStream();
}
I used
BufferedReader buffer = new BufferedReader(
new InputStreamReader(inputStream));
String s="";
while ((s=buffer.readLine())!=null){
stringBuffer.append(s);
System.out.println(s);
}
to make sure that I am reading the xml successfully and it turns out that the code is running properly. Next, I am using the following code parse the xml
XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlFactoryObject.newPullParser();
myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES
, false);
myparser.setInput(inputStream, null);
int event = myparser.getEventType();
while (event != XmlPullParser.END_DOCUMENT){
Log.d("XMLPullParserHandler", "sdfdsf " + myparser.getName());
String name = myparser.getName();
But I am getting java.lang.NullPointerException at myparser.getName(); also the above log is showing "sdfdsf null".
What am I doing wrong?