tags:

views:

741

answers:

2

Hi, I am using KSOAP2 to call a .NET webservice from android application,and the response from the web service is in the following format

anyType{
UserName=anyType{}; 
Password=anyType{}; 
ApplicationCode=JOB; 
ActionType=Query; 
MessageParameters=anyType{Parameters=anyType{}; }; 
TableData=anyType{TableNo=167; 
          TableName=Job; 
   DataRows=
   anyType{
  DataRow=
    anyType{
       DataRowValues=
   anyType{
       DataRowValue=
    anyType{
     FieldNo=1; 
     FieldName=No.; 
     PrimaryKey=true; 
     FieldType=Code20; DataValue=DEERFIELD, 8 WP; 
           };
      DataRowValue=
    anyType
           {
     FieldNo=3; 
     FieldName=Description; 
     PrimaryKey=false; 
     FieldType=Text50; 
     DataValue=Setting up Eight Work Areas; 
           };
    DataRowValue=
    anyType
           {
     FieldNo=4; 
     FieldName=Description 2; 
     PrimaryKey=false; 
     FieldType=Text50; 
     DataValue=anyType{}; 
           }; 
    }; 
     }; 
     }; 
    }; 
 }; 
 ResponseForRequest=GETTABLEDATA; 
 CustomIdentifier=TestBB; 
Applications=anyType{}; 
Forms=anyType{}; 
Menu=anyType{}; 
}

I am not aware about the format of this response and i don't know how to parse this response to get particular result.Any one knows about it please help me.

Note: i manually formatted this response for your understanding.

A: 

I dont recognise the format. I think you are going to have to parse the reponse yourself,

A series of regex's seems the quickest start.

eg:

String intput = ""; //your big response string
List<Map<String,String>> rows = new ArrayList<Map<String,String>>();
String[] rowdata = input.matches("DataRowValue\=\r\s*anyType{[^}]*};");


for (String r : rowdata){
   Map<String, String> row = new HashMap<String, String>();
   String[] nvpairs = r.split(";");

   for (string pair : nvpairs) {
      String[] s = pair.split("=");
      row.push(s[0], s[1]);
   }

}

should get you started. You will probably need to adjsut the first regex for lots of reasons. something like "(?<=DataRowValue=[^{])[^}]" might be more approriate. I'd be tempted to access anything that only appears once by fishing it out directly with something like

String username = input.match("(?<=UserName\=)[^;]*")

NSherwin
+1  A: 

Actually this a known format if you know Java Script.These data in this format are infact JSON Object's and JSON Array's. I hope you are using the KSOAP2 library.So here is how you can parse this result.

eg:

private Bundle bundleResult=new Bundle();
private JSONObject JSONObj;
private JSONArray JSONArr;
Private SoapObject resultSOAP = (SoapObject) envelope.getResponse();
/* gets our result in JSON String */
private String ResultObject = resultSOAP.getProperty(0).toString();

if (ResultObject.startsWith("{")) { // if JSON string is an object
    JSONObj = new JSONObject(ResultObject);
    Iterator<String> itr = JSONObj.keys();
    while (itr.hasNext()) {
        String Key = (String) itr.next();
        String Value = JSONObj.getString(Key);
        bundleResult.putString(Key, Value);
        // System.out.println(bundleResult.getString(Key));
    }
} else if (ResultObject.startsWith("[")) { // if JSON string is an array
    JSONArr = new JSONArray(ResultObject);
    System.out.println("length" + JSONArr.length());
    for (int i = 0; i < JSONArr.length(); i++) {
        JSONObj = (JSONObject) JSONArr.get(i);
        bundleResult.putString(String.valueOf(i), JSONObj.toString());
        // System.out.println(bundleResult.getString(i));
    } 
}

Initially i had a lot of trouble with this kind of data but finally i got it all working.From then I have been using this.I hope this helps you solve your problem.

HaKr