0

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.

4

1 回答 1

5

基本上是这样的:

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

这包含 SoapObjects 中的 SoapObjects,因此最好的办法是编写一个递归方法来扫描所有属性并找到您需要的信息。像这样的东西:

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();
        }
    }
}
于 2013-07-31T10:57:14.213 回答