[Android Studio] MXG123:
Hi Folks.

I am new to android studio and object oriented programming in general so apologies if this is obvious but I cannot get my head round it. I have a small xml web server and I can connect to it and send data to it and I can also read it back and view it in the monitor. I want to do xml parsing on it but cannot get it to work.

Below is the xml server being displayed in the android monitor and is from the line "System.out.println(output);"

The response is saved from a string but I think I need it in a different format to do a pull parser on it. The program basically prints this string and then crashes. What is the best way to parse my data? Any help would be really appreciated.

The code:

public class HTTPRequestTask extends AsyncTask<String , Void, String > {
@Override
protected String doInBackground(String... args) {
String IP = args[0];
System.out.println(IP);
try {
URL url = new URL(IP);
XmlPullParser recievedData = XmlPullParserFactory.newInstance().newPullParser();
recievedData.setInput(url.openStream(),null);
System.setProperty("http.keepAlive", "false");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");

if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}

BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));

String output;

System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);

}
XmlPullParser parser = Xml.newPullParser();
parser.setInput(new StringReader(output));

System.out.println("doc");
System.out.println(parser);
System.out.println("Disconnecting\n");
conn.disconnect();
//System.out.println(recievedData);
processRecievedData(recievedData);

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (XmlPullParserException e) {
e.printStackTrace();

}

return null;
}

private int processRecievedData(XmlPullParser xmlData) {
int recordsFound = 0; // Find values in the XML records
int eventType = -1;
String appId = ""; // Attributes
String itemId = "";
String timeStamp = ""; String data = ""; // Text int eventType = -1;
while (eventType != XmlResourceParser.END_DOCUMENT) {
String tagName = xmlData.getName();
switch (eventType) {
case XmlResourceParser.START_TAG: // Start of a record, so pull values encoded as attributes.
if (tagName.equals("Version")) {
System.out.println("yes");
appId = xmlData.getAttributeValue(null, "Model");
itemId = xmlData.getAttributeValue(null, "vendor_id");
timeStamp = xmlData.getAttributeValue(null, "timestamp");
data = "";
}
System.out.println("no");
break;
// Grab data text (very simple processing)
// NOTE: This could be full XML data to process.
case XmlResourceParser.TEXT:
data += xmlData.getText();
break;

case XmlPullParser.END_TAG:
if (tagName.equals("record")) {
recordsFound++;
//publishProgress(appId, itemId, data, timeStamp);
}
break;
}
//eventType = xmlData.next();
}
// Handle no data available: Publish an empty event.
if (recordsFound == 0) { publishProgress();
}
Log.i(TAG, "Finished processing "+recordsFound+" records.");
return recordsFound;

}

protected void onProgressUpdate(String... values) {
if (values.length == 0) {
Log.i(TAG, "No data downloaded");
}
if (values.length == 4) {
String appId = values[0];
String itemId = values[1];
String data = values[2];
String timeStamp = values[3];
// Log it
Log.i(TAG, "AppID: " + appId + ", Timestamp: " + timeStamp);
Log.i(TAG, " ItemID: " + itemId + ", Data: " + data);
// Pass it to the application handleNewRecord(itemId, data); }
//super.onProgressUpdate(values);
}
}

Thanks.