其他分享
首页 > 其他分享> > 在android中解析SoapObject Responst

在android中解析SoapObject Responst

作者:互联网

我的代码是:

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

我在logcat中的回答是:

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

     };
    };
   };
 }

我想从此响应中获取RoleID和RoleName.

我尝试过使用result.getAttribute(0)和result.getProperty(0).但他们没有帮助.我也google了很多.解析此响应的任何帮助都将受到高度关注.

谢谢.

解决方法:

基本上它是这样的:

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

标签:android,android-ksoap2,wcf
来源: https://codeday.me/bug/20190929/1832380.html