0

我正在使用 ksoap2 在我的 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;

    }

}   

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

谢谢!

4

3 回答 3

1

您的 SOAP_ACTION 应该是这样的。

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

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

请参阅此问题的答案以获取详细说明。

于 2012-07-05T06:14:00.480 回答
1

你也可以试试这个。

  SoapPrimitive resultSoapPrimitive;
        resultSoapPrimitive = (SoapPrimitive) envelope.getResponse();
        if (resultSoapPrimitive != null) {
            result = resultSoapPrimitive.toString();
于 2012-07-05T06:18:13.630 回答
1

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

于 2012-07-05T06:43:37.773 回答