0


I am encounter a problem. I have following xml

<string>[{"BatchIDs":[],"HomeWorkCategoryName":"","FileURL":"","FileName":"","ID":1,"Title":"test","Description":"test","HomeworkCategoryID":1,"ExpiryDate":"\/Date(1386658800000)\/","FileID":-2147483648,"URL":"","Mode":1,"Type":2,"Status":1,"CreatedOnDate":"\/Date(1388093500883)\/","UpdatedOnDate":"\/Date(1388093500883)\/","Inactive":false,"Deleted":true},
{"BatchIDs":[],"HomeWorkCategoryName":"","FileURL":"","FileName":"","ID":1,"Title":"test","Description":"test","HomeworkCategoryID":1,"ExpiryDate":"\/Date(1386658800000)\/","FileID":-2147483648,"URL":"","Mode":1,"Type":2,"Status":1,"CreatedOnDate":"\/Date(1388093500883)\/","UpdatedOnDate":"\/Date(1388093500883)\/","Inactive":false,"Deleted":true},
{"BatchIDs":[],"HomeWorkCategoryName":"","FileURL":"","FileName":"","ID":1,"Title":"test","Description":"test","HomeworkCategoryID":1,"ExpiryDate":"\/Date(1386658800000)\/","FileID":-2147483648,"URL":"","Mode":1,"Type":2,"Status":1,"CreatedOnDate":"\/Date(1388093500883)\/","UpdatedOnDate":"\/Date(1388093500883)\/","Inactive":false,"Deleted":true}]

And I want to read all values passed in this xml. This xml is single string return by web service. Currently I am using following code (it's also providing null value for first entry)

Object result = envelope.getResponse();
str=result+"";
String key,value;
String[] couple = str.split(",\"");
for(int i =0; i < couple.length ; i++) {
String[] items =couple[i].split(":");
key=items[0];
value=items[1];
key=key.replaceAll("\"", "");                  
value=value.replaceAll("\"", "");
/* some conditions to fetch values */
}

Please tell me how can I get exact values and keys in android.
Thanks,

2 Answers2

0

It seems to be a valid json. You can use http://jsonlint.com/ to check that.

So with this following class : JSONArray, you can retrieve what you want.

mrroboaat
  • 5,602
  • 7
  • 37
  • 65
0

Thanks everyone for helping me.
Here is my soluction through I overcome my problem. May be it will use for others

HttpGet httpRequest = new HttpGet(_URL);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpresponse = httpclient.execute(httpRequest);
JSONArray response = null;


 try {
    response = new JSONArray(getJSONString(httpresponse));
    } catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


 for (int i = 0; i < response.length(); i++) {
    try {
    //your values
    String _name=response.getJSONObject(i).getString("NAME");
    }
    } catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
}

Here is 2nd function.If your function is parse through xml code.

public static String getJSONString(HttpResponse response) {
    try {
         DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
         Document doc = builder.parse(response.getEntity().getContent());
         NodeList nl = doc.getElementsByTagName("string");
         Node n = nl.item(0);
         String str = n.getFirstChild().getNodeValue();
         return str;
    } catch (ParserConfigurationException e) {
         e.printStackTrace();
    } catch (SAXException e) {
         e.printStackTrace();
    } catch (IllegalStateException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 return null;
}


Happy Coding