Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to consume this web service: http://www.ezzylearning.com/services/CountryInformationService.asmx with KSoap.

When I call a web method (example: GetContinents) without parameters it work fine. When I try to use one with parameters it always return empty data.

Here is my Activity class (for testing purposes):

public class MyTestActivity extends Activity
{
      private static final String NAMESPACE = "http://www.ezzylearning.com/services/";
      private static final String URL = "http://www.ezzylearning.com/services/CountryInformationService.asmx?WSDL";
      private static final String ACTION_GET_COUNTRIES_BY_CONTINENT = "http://www.ezzylearning.com/services/CountryInformationService.asmx/GetCountriesByContinent";
      private static final String METHOD_GET_COUNTRIES_BY_CONTINENT = "GetCountriesByContinent";    

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
     {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fourth);
    String property = "continentCode";
    String value = "AS";
    new GetCountriesByContinentTask().execute(URL, NAMESPACE, ACTION_GET_COUNTRIES_BY_CONTINENT, METHOD_GET_COUNTRIES_BY_CONTINENT, property, value);
   }

// Implementation of AsyncTask used get the countries by continent.
private class GetCountriesByContinentTask extends AsyncTask<String, Void, String>
{

    @Override
    protected void onPreExecute()
    {
    }

    @Override
    protected String doInBackground(String... params)
    {
        String result = "Testing...";

        String url = params[0];
        String namespace = params[1];
        String action = params[2];
        String method = params[3];
        String property = params[4];
        String value = params[5];
        List<PropertyInfo> propertyList = new ArrayList<PropertyInfo>();

        //Use this to add parameters
        PropertyInfo pi = new PropertyInfo();
        pi.setName(property);
        pi.setValue(value);
        pi.setType(String.class);
        propertyList.add(pi);

        try
        {   
            MySoapHandler soapHandler = new MySoapHandler();
            SoapObject soapRresult = soapHandler.soapCall(url, namespace, action, method, propertyList);
            if (soapRresult != null)
            {
                System.out.println("RESULT: " + soapRresult.toString());  // THE DATA SET IS ALWAYS EMPTY     
            }
        } 
        catch (Exception ex) 
        {
            System.out.println("Exception: " + ex.getMessage());       
            ex.printStackTrace();
        }       

        return result;
    }

    @Override
    protected void onPostExecute(String result) 
        {
        }
    }
}


My Class for calling the web service:

public class MySoapHandler
{ 
        public SoapObject soapCall(String url, 
                               String namespace, 
                               String action, 
                               String method, 
                               List<PropertyInfo> propertyInfoList)
    throws IOException, XmlPullParserException
    {
        //Initialize soap request
    SoapObject request = new SoapObject(namespace, method);

    //Add parameters
    if(propertyInfoList != null)
    {
        for(PropertyInfo pi: propertyInfoList)
        {
            request.addProperty(pi);
        }
    }

    //Declare the version of the SOAP request
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);
    envelope.dotNet = true;

    //Call the webservice
    HttpTransportSE transport = new HttpTransportSE(url);
    transport.call(action, envelope);

    //Return the SoapResult from the envelope body.

return (SoapObject)envelope.bodyIn;
    }
}


Any idea of what I'm doing wrong?

I also tried to change envelope.bodyIn to envelope.getResponse(), but the dataset is still empty.

Thank you.
Posted

Java
package com.example.ss;

import android.app.Activity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.app.ProgressDialog;
import android.widget.TextView;
import android.widget.Toast;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;


/**
 * Created by seguro6 on 6/14/13.
 */
public class sampletest extends Activity {
    private static final String SOAP_ACTION = "http://www.ezzylearning.com/services/CountryInformationService.asmx/GetCountriesByContinent";
    private static final String METHOD_NAME = "GetCountriesByContinent";
    private static final String NAMESPACE = "http://www.ezzylearning.com/services/CountryInformationService.asmx";
    private static final String URL = "http://www.ezzylearning.com/services/CountryInformationService.asmx";
    Object response;
    TextView displayTextview;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        displayTextview=new TextView(this);
        setContentView(displayTextview);
        new Dowork().execute();
    }

    public class Dowork extends AsyncTask<string,object,object>
    {
        private final ProgressDialog dialogue =new ProgressDialog(sampletest.this);
        @Override
        protected void onPreExecute()
        {
            this.dialogue.setMessage("Checking..");
            this.dialogue.show();
        }
        @Override
        protected void onCancelled(Object result)
        {
            super.onCancelled();
        }
        @Override
        protected void onPostExecute(Object result)
        {
            if(result!=null)
            {

                displayTextview.setText(result.toString());
            }
            else
            {
                Toast.makeText(getApplicationContext(),result.toString(),Toast.LENGTH_LONG).show();
            }
            if (this.dialogue.isShowing()) {

                this.dialogue.dismiss();
            }
        }
        @Override
        protected Object doInBackground(String ...params)
        {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("continentCode","AF");
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet=true;
            envelope.setOutputSoapObject(request);
            try
            {
                AndroidHttpTransport aht=new AndroidHttpTransport(URL);
                aht.call(SOAP_ACTION,envelope);
                request=(SoapObject)envelope.getResponse();
                response=request.toString();
                return response;
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
            return response;
        }

    }
}
 
Share this answer
 
Comments
ricardo.bicho@gmail.com 14-Jun-13 15:23pm    
Thanks a lot. It worked perfectly.
My Namespace (http://www.ezzylearning.com/services/) was incorrect.
josh-jw 17-Jun-13 0:05am    
welcome.
 
Share this answer
 
Comments
ricardo.bicho@gmail.com 13-Jun-13 8:10am    
Thank you for the link, but that did not solve my problem. The article is basically what I'm doing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900