如何向SoapObject添加类属性?

时间:2014-05-06 09:26:50

标签: android wcf ksoap2

我是android编程的新手。 (我的英语不好。如果你发现错误,我道歉。)
所以,我的wcf服务上课:

public class Credential
{
    public string Login {get;set;}
    public string Password {get;set;}
}

以及检查此凭据的方法:

public bool CheckUserCredential(Credential credential)
{
    //some code for checking credential
    return result;
}

和android的代码:

private static final String METHOD_NAME = "CheckUserCredential";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String SOAP_ACTION 
                           = "http://tempuri.org/ISelfCareMobileService/CheckUserCredential";

private static final String URL = "http://10.0.2.2:5795/SelfCareMobileService.svc"; 

private static final int SOAP_VERSION = SoapEnvelope.VER11; 
public boolean Login()
{
    try 
    {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        //Here I should add my wcf class like argument to SoapObject
        request.addProperty("Credential", Credential);

        SoapSerializationEnvelope envelope 
                                 = new SoapSerializationEnvelope(SOAP_VERSION);
        envelope.dotNet = true;         
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, 60000);
        androidHttpTransport.debug = true;
        androidHttpTransport.call(SOAP_ACTION, envelope);
        Object result = envelope.getResponse();

        return true;
    } 
    catch (Exception e) 
    {
        return false;
    }       
}

我的课程是在wcf服务中。如何在android中创建类的实例或如何将类添加到类似属性的SoapObject?我该如何实施呢?

1 个答案:

答案 0 :(得分:0)

只有3种默认类型PropertyInfo,因此必须根据您的SoapObject

创建Soap XML structure
1.PropertyInfo.STRING_CLASS

2.PropertyInfo.INTEGER_CLASS

3.PropertyInfo.OBJECT_CLASS

但是对于Soap Parsing,我们可以像Gson中的Json Parsing一样传递Class类型

使用此SoapResponseParser课程。

import java.lang.reflect.Field;
import java.lang.reflect.Type;

public class SoapResponseParser {

    public static void parseBusinessObject(String input, Object output)
            throws NumberFormatException, IllegalArgumentException,
            IllegalAccessException, InstantiationException {

        @SuppressWarnings("rawtypes")
        Class theClass = output.getClass();
        Field[] fields = theClass.getDeclaredFields();

        for (int i = 0; i < fields.length; i++) {
            Type type = fields[i].getType();
            fields[i].setAccessible(true);

            // detect String
            if (fields[i].getType().equals(String.class)) {
                String tag = fields[i].getName() + "=";

                if (input.contains(tag)) {
                    String strValue = input.substring(
                            input.indexOf(tag) + tag.length(),
                            input.indexOf(";", input.indexOf(tag)));
                    if (strValue.length() != 0) {
                        if (strValue.contains("anyType")) {
                            fields[i].set(output, "-");
                        } else
                            fields[i].set(output, strValue);
                    }
                }
            }

            // detect int or Integer
            if (type.equals(Integer.TYPE) || type.equals(Integer.class)) {
                String tag = fields[i].getName() + "=";

                if (input.contains(tag)) {
                    String strValue = input.substring(
                            input.indexOf(tag) + tag.length(),
                            input.indexOf(";", input.indexOf(tag)));
                    if (strValue.length() != 0) {
                        fields[i].setInt(output, Integer.valueOf(strValue));
                    }
                }
            }

            // detect float or Float
            if (type.equals(Float.TYPE) || type.equals(Float.class)) {
                String tag = fields[i].getName() + "=";
                if (input.contains(tag)) {
                    String strValue = input.substring(
                            input.indexOf(tag) + tag.length(),
                            input.indexOf(";", input.indexOf(tag)));
                    if (strValue.length() != 0) {
                        fields[i].setFloat(output, Float.valueOf(strValue));
                    }
                }
            }
            // detect double or Double
            if (type.equals(Double.TYPE) || type.equals(Double.class)) {
                String tag = fields[i].getName() + "=";
                if (input.contains(tag)) {
                    String strValue = input.substring(
                            input.indexOf(tag) + tag.length(),
                            input.indexOf(";", input.indexOf(tag)));
                    if (strValue.length() != 0) {
                        fields[i].setDouble(output, Double.valueOf(strValue));
                    }
                }
            }
        }

    }
}

并像

一样使用它
MyCredentialClass credentilas= new MyCredentialClass();
SoapResponseParser.parseBusinessObject(result .toString(),credentilas);