Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

wcf - Parsing SoapObject Responst in android

My code is:

public class MainActivity extends Activity implements OnClickListener {

Button b;
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "GetList";
private static String SOAP_ACTION = "http://tempuri.org/IWCFMasterRole/GetList";
private static String URL = "http://172.16.0.1:55355/WCFMasterRole.svc";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.button1) {
        new MyClass().execute("");
    }
}

class MyClass extends AsyncTask<String, Void, SoapObject> {
    SoapObject result;
    @Override
    protected SoapObject doInBackground(String... params) {
        try {

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            // request.addProperty("CityId", "CITY0001");
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            HttpTransportSE transport = new HttpTransportSE(URL);
            try {
                transport.call(SOAP_ACTION, envelope);
            } catch (Exception e) {
                e.printStackTrace();
            }
            result = (SoapObject) envelope.bodyIn;
            System.out.println("Result is : " + result);
        } catch (Exception e) {
            System.out.println("Exception : "+e.toString());
        }

        return result;
    }
}
}

And my response in logcat is:

    Result is : 
GetListResponse
 {
   GetListResult=anyType
   {schema=anyType
    {element=anyType
     {complexType=anyType
      {choice=anyType
       {element=anyType
        {complexType=anyType
         {sequence=anyType
          {element=anyType{}; element=anyType{}; element=anyType{}; 
          }; 
         }; 
        }; 
       }; 
      }; 
     }; 
    }; 
    diffgram=anyType
    {DocumentElement=anyType
     {Table1=anyType

      {RoleID=ROLEAAAA0000; RoleName=Administrator; }; 
      Table1=anyType
      {RoleID=ROLEAAAA0001; RoleName=Developer; }; 
      Table1=anyType
      {RoleID=ROLEAAAA0003; RoleName=Senior Developer; };
      Table1=anyType
      {RoleID=ROLEAAAA0004; RoleName=Junior Developer; }; 
      Table1=anyType
      {RoleID=ROLEAAAA0005; RoleName=Trainee; }; 

     };
    };
   };
 }

I want to get RoleID and RoleName from this response.

I have tried with result.getAttribute(0) and result.getProperty(0). But They didn't help. Also I have googled lot. Any help to parse this response will be highly appriciated.

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Basicly it's something like this:

SoapObject GetListResponse = (SoapObject)result.getProperty(0); 
SoapObject DocumentElement = (SoapObject)GetListResponse.getProperty(3);
SoapObject Table1 = (SoapObject)DocumentElement.getProperty(0);

This contains SoapObjects within SoapObjects, so the best thing to do is to write a recursive method to scan through all properties and find the information you need. Something like this:

private static void ScanSoapObject(SoapObject result) 
{
    for(int i=0;i<result.getPropertyCount();i++)
    {
        if(result.getProperty(i) instanceof SoapObject)
        {               
             ScanSoapObject((SoapObject)result.getProperty(i));
        }
        else
        {               
            //do something with the current property

            //get the current property name:
            PropertyInfo pi = new PropertyInfo();
            result.getPropertyInfo(i,pi);
            String name = pi.getName();
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...