如何使用kso​​ap2获取Web服务响应

时间:2012-07-03 22:18:16

标签: android web-services android-ksoap2

我正在使用kso​​ap2在我的Android应用中调用Web服务。它工作了很多次,但不知怎的,我无法通过这个。我使用下面提到的另一个类从我的活动中调用了一个Web服务:

public class ServiceCall {
    public static String loginID; 
public static String password; 
public static String authStatus;
public static String firstName;
public static String lastName;

private static final String SOAP_ACTION = "http://www.tempuri.us/";
private static final String NAMESPACE = "http://www.tempuri.us/";
private static final String URL = "http://test.tempuriprojects.com/WebServices/GetWorkData.asmx";

private boolean isResultVector = true;

protected Object call(String soapAction, SoapSerializationEnvelope envelope){
    Object result = null;


    final HttpTransportSE transportSE = new HttpTransportSE(URL);

    transportSE.debug = false;


    //call and Parse Result.        
    try{
        transportSE.call(soapAction, envelope);


        if (!isResultVector){
            result = envelope.getResponse();
            Log.e("RESULT: ", result.toString());
        } else {
            result = envelope.bodyIn;
            Log.e("RESULT1: ", envelope.bodyIn.toString());
        } 
    } catch (final IOException e){
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (final Exception e){
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return result;
}


    public ArrayList<DashboardEntry> getDashboardEntries(DashboardEntryCriteria bean){
    ArrayList<DashboardEntry> dashboardEntryList = new ArrayList<DashboardEntry>();
    try{
        String methodName = "GetDashboardEntries";

        // Create the outgoing message
        final SoapObject requestObject = new SoapObject(NAMESPACE, methodName);

        bean.setLoginID("100");
        bean.setPassword("12345");

        PropertyInfo propertyInfo = new PropertyInfo();
        propertyInfo.setName("dashboardentries");
        propertyInfo.setType(bean.getClass());
        propertyInfo.setValue(bean);
        requestObject.addProperty(propertyInfo);

        //create soap envelop : soap version 1.1 
        final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.xsd = "http://tempuri.org/GetDashboardEntries.xsd";
        envelope.addMapping(null, "dashboardentries", new DashboardEntryCriteria().getClass());
        envelope.implicitTypes = true;

        // add the outgoing object as the request
        envelope.setOutputSoapObject(requestObject);

        // call and Parse Result.
        final Object response = this.call(SOAP_ACTION + methodName, envelope);

        SoapObject soapObj = (SoapObject)response;

        int j = 0;
        for(int i=0;  i < length; i++)
        {

            SoapObject result = (SoapObject) soapObj.getProperty(i);

            if(soapObj != null ){

            if(result.hasProperty("AuthStatus") && String.valueOf(result.getProperty("AuthStatus").toString()).equals("Y")){

                loginID = result.getProperty("LoginID").toString(); 
                System.out.println(loginID);
                password = result.getProperty("Password").toString(); 
                System.out.println(password);
                authStatus = result.getProperty("AuthStatus").toString();
                System.out.println(authStatus);

                // Irrelevant code
            }   

            }

        }
    }

    catch(Exception e){
        Log.e("Exception: ", e.toString());
    }
    return dashboardEntryList ;
}

这是DashboardEntryCriteria bean:

public class DashboardEntryCriteria implements KvmSerializable{

private String LoginID;
private String Password;



/**
 * @return the password
 */
public String getPassword() {
    return Password;
}

/**
 * @param password the password to set
 */
public void setPassword(String password) {
    Password = password;
}


/**
 * @return the loginID
 */
public String getLoginID() {
    return LoginID;
}

/**
 * @param loginID the loginID to set
 */
public void setLoginID(String loginID) {
    LoginID = loginID;
}

}

@Override
public Object getProperty(int arg0) {

    switch(arg0){
    case 0 : return LoginID;
    case 1 : return Password;

    }
    return null;

}

@Override
public int getPropertyCount() {
    // TODO Auto-generated method stub
    return 2;
}

@SuppressWarnings("rawtypes")
@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {

    switch(arg0){
    case 0 : 
        arg2.name = "LoginID";
        arg2.type = PropertyInfo.STRING_CLASS;
        arg2.namespace = "http://tempuri.org/RxGetWorkEntries.xsd";
        break;
    case 1 : 
        arg2.name = "Password";
        arg2.type = PropertyInfo.STRING_CLASS;
        arg2.namespace = "http://tempuri.org/RxGetWorkEntries.xsd";
        break;

    }

}

@Override
public void setProperty(int arg0, Object arg1) {

    switch(arg0){
    case 0 : 
        LoginID = arg1.toString();
        break;
    case 1 : 
        Password = arg1.toString();
        break;

    }

}   

}  我很肯定错误发生在方法getDashboardEntries()中的某个ServiceCall类中。我注意到,当调用方法call时,它会给出空响应(或“结果”对象)。我尝试使用具有相同参数的soapUI调用Web服务,它工作得很好。只是为了让您知道Web服务没有任何问题。那么任何想法还有什么可能是错的或缺失的?

谢谢!

3 个答案:

答案 0 :(得分:1)

你的SOAP_ACTION应该是这样的。

AppConsts.NAMESPACE =  "http://www.tempuri.us/"

usecaseString =这应该是您的方法服务名称 String soapAction = AppConsts.NAMESPACE + usecaseString;

see回答这个问题以获得详细解释。

答案 1 :(得分:1)

您也可以试试这个。

  SoapPrimitive resultSoapPrimitive;
        resultSoapPrimitive = (SoapPrimitive) envelope.getResponse();
        if (resultSoapPrimitive != null) {
            result = resultSoapPrimitive.toString();

答案 2 :(得分:1)

我会向您推荐this教程。完成此操作后,您将能够理解完整的概念。