Chleung49
Chleung49

Reputation: 41

How to parse the ksoap2 response returned from web service in android development?

After the web method is called, a XML string is returned as the response. the android side code is like that :

SoapPrimitive response = (SoapPrimitive) ws.envelope.getResponse();

and the xml is like:

<string xmlns="http://tempuri.org/">
[{"ID":"[email protected]","float_latitude":22.338,"float_longitude":114.169},          {"ID":"[email protected]","float_latitude":22.33974,"float_longitude":114.169456},    {"ID":"[email protected]","float_latitude":22.3384857,"float_longitude":114.1691},    {"ID":"[email protected]","float_latitude":50,"float_longitude":100}]
</string>

I want to get

  1. the number of id returned
  2. save the lat and long with regards to id to my local variables

Upvotes: 0

Views: 1141

Answers (1)

JakeWilson801
JakeWilson801

Reputation: 1036

O man. First of all your response isn't XML it is JSON. The way I parse JSON is first build a generic object.

 public class yourObject{

  private String id; 
  private double lat;
  private double lng; 
 }

From there build out a collection an arraylist should be fine.

  ArrayList<yourObject> objects = new ArrayList<yourObject>(); 

After that do some research into JSON here is some starter code.

 JSONArray myAwarry = new JSONArray(response);

pass in the string that gets returned from the server eg response.

iterate through the array

 for(int i = 0; i < myAwarry.length(); i++){
 JSONObject o = myAwarry.get(i); 
 yourObject obj = new yourObject(); 
 obj.setId(o.getString("ID")); 
 obj.setlat(o.getDouble("float_longitude")); 
 obj.setlng(o.getDouble("float_latitude")); 
 //add the obj to the collections of objects
  objects.add(obj); 

}

Hope this helps

Upvotes: 2

Related Questions