How to parse this web service response in Android?

I am using KSOAP2 to call the .NET webservice from an 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 do not know about the format of this answer, and I do not know how to parse this answer to get a specific result. Anyone know about this, please help me.

Note. I manually formatted this answer for your understanding.

+5
source share
5 answers

This is actually a well-known format if you know Java Script. This data in this format is infact JSON Objectand JSON Array. Hope you are using KSOAP2 library. 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));
    } 
}

, , , . . , .

+7

, SOAP JSON; , "{", "anyType".

"anyType", JSON. IndexOf ( "{" ); , JSON, .

, , JSON.

: Android KSoap2:

, :

    public Bundle getElementsFromSOAP(SoapObject so){
    Bundle resultBundle = new Bundle();
    String Key = null;
    String Value = null;
    int elementCount = so.getPropertyCount();                  

    for(int i = 0;i<elementCount;i++){
        PropertyInfo pi = new PropertyInfo();
        SoapObject nestedSO = (SoapObject)so.getProperty(i);

        int nestedElementCount = nestedSO.getPropertyCount();
        Log.i(tag, Integer.toString(nestedElementCount));

        for(int ii = 0;ii<nestedElementCount;ii++){
            nestedSO.getPropertyInfo(ii, pi);
            resultBundle.putString(pi.name, pi.getValue().toString());
            //Log.i(tag,pi.getName() + " " + pii.getProperty(ii).toString());
            //Log.i(tag,pi.getName() + ": " + pi.getValue());

        }
    }

    return resultBundle;

}
+5

, :

anyType
{
  FOO_DEALS=anyType
  {
       CATEGORY_LIST=anyType
       {
         CATEGORY=Books; 
         CATEGORY_URL=books_chennai; 
         CATEGORY_ICON=http://deals.foo.com/common/images/books.png; 
         CATEGORY_COUNT=1045; 
         TYPE=1; 
         SUPERTAG=Books; 
       };
       CATEGORY_LIST=anyType
       {
           CATEGORY=Cameras;
           CATEGORY_URL=cameras_chennai;
           CATEGORY_ICON=http://deals.foo.com/common/images/cameras.png; 
           CATEGORY_COUNT=152; 
           SUPERTAG=Cameras; 
           TYPE=1; 
       }; 
   }; 
 }

:

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
           // Add the input required by web service
           request.addProperty("city","chennai");
           request.addProperty("key","10000");

           SoapSerializationEnvelope envelope =new SoapSerializationEnvelope(SoapEnvelope.VER11);
           envelope.setOutputSoapObject(request);

           // Make the soap call.
           androidHttpTransport.call(SOAP_ACTION, envelope);

           // Get the SoapResult from the envelope body.
           resultRequestSOAP = (SoapObject) envelope.bodyIn;


           System.out.println("********Response : "+resultRequestSOAP.toString());

           SoapObject root = (SoapObject) resultRequestSOAP.getProperty(0);
           SoapObject s_deals = (SoapObject) root.getProperty("FOO_DEALS");

           StringBuilder stringBuilder = new StringBuilder();

           System.out.println("********Count : "+ s_deals.getPropertyCount());

           for (int i = 0; i < s_deals.getPropertyCount(); i++) 
           {
               Object property = s_deals.getProperty(i);
               if (property instanceof SoapObject)
               {
                   SoapObject category_list = (SoapObject) property;
                   String CATEGORY = category_list.getProperty("CATEGORY").toString();
                   String CATEGORY_URL = category_list.getProperty("CATEGORY_URL").toString();
                   String CATEGORY_ICON = category_list.getProperty("CATEGORY_ICON").toString();
                   String CATEGORY_COUNT = category_list.getProperty("CATEGORY_COUNT").toString();
                   String SUPERTAG = category_list.getProperty("SUPERTAG").toString();
                   String TYPE = category_list.getProperty("TYPE").toString();
                   stringBuilder.append
                   (
                        "Row value of: " +(i+1)+"\n"+
                        "Category: "+CATEGORY+"\n"+
                        "Category URL: "+CATEGORY_URL+"\n"+
                        "Category_Icon: "+CATEGORY_ICON+"\n"+
                        "Category_Count: "+CATEGORY_COUNT+"\n"+
                        "SuperTag: "+SUPERTAG+"\n"+
                        "Type: "+TYPE+"\n"+
                        "******************************"
                   );                   
                   stringBuilder.append("\n");
               }
           }
+3

. , ,

.

:

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]);
   }

}

. , . - "(? <= DataRowValue = [^ {]) [^}]" . , , -

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

0
source

SoapObject response = (SoapObject) envelope.getResponse ();

          int cols = response.getPropertyCount();

            for (int i = 0; i < cols; i++) {
                Object objectResponse = (Object) response.getProperty(i);




                SoapObject r =(SoapObject) objectResponse;

                FieldName=(String) r.getProperty("FieldName").toString();

                // Get the rest of your Properties by 
                // (String) r.getProperty("PropertyName").toString();

            }
0
source

All Articles