Search code examples
androidweb-servicesandroid-ksoap2

How to get a web service response using ksoap2


I'm using ksoap2 to call web services in my android app. It worked on quite a few occasions but somehow I'm not able to get past this one. I call a web service from my activity using another class mentioned below:

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

This is the 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;

    }

}   

} I'm positive that the error is in the class ServiceCall somewhere in the method getDashboardEntries(). I noticed that when the method call is called it gives the empty response (or "result" object). I did try calling the web service using soapUI with the same parameters, it works just fine. Just to let you know that there isn't anything wrong with the web service. So any idea what else could be wrong or missing?

Thank you!


Solution

  • Your SOAP_ACTION should be something like this.

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

    usecaseString = THIS SHOULD BE YOUR METHOD NAME OF SERVICE.
    String soapAction = AppConsts.NAMESPACE + usecaseString;

    see answer of this question to get detailed explanation.