访问complexType元素(Android ksoap2 WCF)

时间:2016-03-29 14:47:06

标签: android wcf ksoap2 android-ksoap2 complextype

在我的Web服务上,有一种我试图访问的方法(CheckCredentials),它需要一个类(或者一个对象)。

我正在尝试访问Web服务中的complexType元素,现在我正处于相当困惑的地步。我使用此链接作为指导来帮助我:http://seesharpgears.blogspot.co.uk/2010/10/ksoap-android-web-service-tutorial-with.html

我不确定如何以及何时插入addProperty作为用户名和密码。

任何帮助都将不胜感激。

网络服务

以下是Web Serivice的相关部分

<xs:element name="CheckCredentials">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="credentials" nillable="true" type="loginCredentials"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="CheckCredentialsResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="CheckCredentialsResult" type="xs:boolean"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:complexType name="loginCredentials">
<xs:sequence>
<xs:element minOccurs="0" name="_password" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="_username" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>

loginCredentials.java

public class loginCredentials implements KvmSerializable {
    public String Username;
    public String Password;

    public loginCredentials(){}

    public loginCredentials(String username, String password) {
        Username = username;
        Password = password;
    }

    public Object getProperty(int arg0) {
        switch (arg0) {
            case 0:
                return Username;
            case 1:
                return Password;
        }
        return null;
    }

    public int getPropertyCount() {
        return 2;
    }

    public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
        switch(index) {
            case 0:
                info.type = PropertyInfo.STRING_CLASS;
                info.name = "Username";
                break;
            case 1:
                info.type = PropertyInfo.STRING_CLASS;
                info.name = "Password";
                break;
            default:break;
        }
    }

    public void setProperty(int index, Object value) {
        switch(index) {
            case 0:
                Username = value.toString();
                break;
            case 1:
                Password = value.toString();
                break;
            default:
                break;
        }
    }
}

.Test4.java

public class Test4 extends Activity {

    //private String string = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">';

    public class SoapTask extends AsyncTask<Void, Void, Void> {
        private final ProgressDialog dialog = new ProgressDialog(Test4.this);
        protected void onPreExecute() {

            this.dialog.setMessage("Checking in...");
            this.dialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            try{
                WebServiceCallExample();
            }catch (Exception e){
                e.printStackTrace();
            }
            return null;
        }

        protected void onPostExecute(Void result) {
            TextView tv = (TextView) findViewById(R.id.Result);
            tv.setText(test_string);
            if (this.dialog.isShowing()){
                this.dialog.dismiss();
            }
        }

    }

    private String test_string;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_test4);
        new SoapTask().execute();
    }

    @SuppressWarnings("deprecation")
    public void WebServiceCallExample() {
        final String NAMESPACE = "http://tempuri.org/";
        final String URL = "http://[IP]/MobileWebService/MobileWebService.MobileService.svc";
        final String METHOD_NAME = "CheckCredentials";
        final String SOAP_ACTION = "http://tempuri.org/IMobileService/CheckCredentials";

        SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
        loginCredentials L = new loginCredentials();

        PropertyInfo pi = new PropertyInfo();
        pi.setName("L");
        pi.setValue(L);
        pi.setType(L.getClass());
        Request.addProperty(pi);

        // "<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'><s:Header><Action s:mustUnderstand='1' xmlns='http://schemas.microsoft.com/ws/2005/05/addressing/none'>http://tempuri.org/IMobileService/CheckCredentials</Action></s:Header><s:Body><CheckCredentials xmlns='http://tempuri.org/'><credentials xmlns:d4p1='http://schemas.datacontract.org/2004/07/MobileWebService' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'><d4p1:_password>Crisps</d4p1:_password><d4p1:_username>C FAIRS</d4p1:_username></credentials></CheckCredentials></s:Body></s:Envelope>"

  /* Set the web service envelope */
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);

        envelope.addMapping(NAMESPACE, "loginCredentials", new loginCredentials().getClass());
        System.out.println("Request::" + Request.toString());


        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;
       // System.out.println(androidHttpTransport.responseDump);
        try{
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapObject sp = (SoapObject) envelope.getResponse();
            L.Username = sp.getProperty(0).toString();
            L.Password = sp.getProperty(1).toString();
            //String
            //SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

            SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
            System.out.println("Response::" + resultsRequestSOAP.toString());
            test_string = sp.toString();
        }catch (Exception e){
           // System.out.println(envelope.getResponse());
            e.printStackTrace();
        } finally {
            System.out.println("Fs1:" + androidHttpTransport.requestDump);
            System.out.println("Fs2" + androidHttpTransport.responseDump);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您不需要为请求创建自定义KvmSerializable类。这是您传递凭据所需的操作。

SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME); 

SoapObject credentialsRequest = new SoapObject(namespace, "credentials"); 
credentialsRequest.addProperty("_password", username); 
credentialsRequest.addProperty("_username", username); 

Request.addSoapObject(credentialsRequest);