SOAP in Android

Like many other Android developers I came to the same problem – parsing SOAP in Android.
My short instruction how to do this…

  1. download ksoap2 sourse files – ksoap2-src-2.1.1.zip (http://sourceforge.net/project/showfiles.php?group_id=158028&package_id=176860)
  2. download kxml2 source files – kxml2-src-2.3.0.zip (http://sourceforge.net/project/showfiles.php?group_id=9157&package_id=58653)
  3. put in your source folder:

    ksoap2-src-2.1.1.zip
    /src/org/ksoap2/
    /src_j2se/org/ksoap2/

    kxml2-src-2.3.0.zip
    /src/org/kxml2/

    take missed kObjects files from here: http://kobjects.cvs.sourceforge.net/viewvc/kobjects/kobjects/src/org/kobjects/
    /org/kobjects/base64/Base64.java
    /org/kobjects/isodate/IsoDate.java

    Download

  4. Use simple code to connect the server
    private static final String SOAP_ACTION = "myMethod";
    private static final String METHOD_NAME = "myMethod";
    private static final String NAMESPACE = "http://mynamespace.com/";
    private static final String URL = "http://myserver.com/bean";
     
    void test() {
        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("prop1", "myprop");
     
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);
     
            Object result = envelope.getResponse();
     
            //handle result here
     
            myExampleHandler.getResults();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Share
  • http://www.piwai.info Piwaï

    I am trying to do exactly the same things, so I would like to thank you for posting this.

    By the way, could you also give an example of ParserHandler (the ContentHandler implementation) ? I am going to search Google, but I think it might be usefull for the next visitors ;) .

  • villain_dm

    ParserHandler extends DefaultHandler-) I think this is a part of regular SAX parser tutorials like this: http://www.anddev.org/parsing_xml_from_the_net_-_using_the_saxparser-t353.html

  • http://www.piwai.info Piwaï

    Thanks for answering so fast. I will have a look at that. Anyway, even without parsing the response, everything works fine (using result.toString() ). So thanks a lot for that !

    One more thing for visitors : if you use local webservices while developping, don’t put localhost or 127.0.0.1 in the url, since it will be the localhost of the Emulator, not the one of your computer. You should put your network IP instead (like 192.168.10.10).

  • villain_dm

    well… actually I didn’t do any research in parsing response-) I replaced soap to the simple XML.
    result.toString() will be something like anyType{name=value; …}… SAX parser won’t help in this case:)

    please see ksoap2 tutorial how to handle this: http://google.com/codesearch/p?hl=en#fNok_iy2ojk/samples/org/ksoap2/samples/amazon/search/AmazonSearchClient.java

  • http://www.piwai.info Piwaï

    Just wanted to give some more feedback :

    Android does not currently works fine with HTTPS and some Certificate Authorities, probably due to a lack of implementation of some algorithms. If you happen to encounter the following IOException : “Hostname was not verified”, here is the crappy patch I found : in org.ksoap2.transport.ServiceConnectionSE, I added the following code on line 50 (after “connection.setUseCaches(false);” in the constructor) :

    if (connection instanceof HttpsURLConnection) {
    ((HttpsURLConnection) connection)
    .setHostnameVerifier(new AllowAllHostnameVerifier());
    }

    THIS IS A TEMPORARY AND BAD SOLUTION, since CA are not any more verified.

  • alicia

    I tried to use your code, and I did not change anything except namespace, url etc. but it didn’t work.

    I tried to debug and every this call : androidHttpTransport.call(SOAP_ACTION, envelope);

    I get an unknown error, and fall catch block.

    I try to connect a dotnet web service, please help me if you know something..

    Below is my func. names etc..

    private static final String SOAP_ACTION = “http://tempuri.org/AddIntegers”;
    private static final String METHOD_NAME = “AddIntegers”;
    private static final String NAMESPACE = “http://tempuri.org/”;
    private static final String URL = “http://127.0.0.1:1416/WebServiceTest/Service.asmx”;

  • Michael

    thank you, i tried to get ksoap working the whole last week….and with your tutorial it worked in a few minutes…
    good job!

  • Adam Gaines

    For some reason no matter what I do I get the following error:

    detailMessage “expected: END_TAG {http://schemas.xmlsoap.org/soap/envelope/}Body (position:END_TAG @1:375 in java.io.InputStreamReader@4377f160) ” (id=830060642608)

  • http://michang05.blogspot.com Mike

    Thanks and i like the blog image headers eheh

  • RTN

    Hi MAte,

    I am new to android platform and not sure which one is SOAP_ACTION, NAMESPACE AND URL.

    I have blow pasted my WSDL please have a look and tell me which one they are.

    thanks


  • Pingback: jKoeber.de » Blog Archive » Android WebService – SOAP / XML RPC

  • Luis Rente

    Hi,
    I’m having a problem accessing my web-service.
    This is my code:

    private static final String SOAP_ACTION = “”;
    private static final String METHOD_NAME = “getPOIS”;
    private static final String NAMESPACE = “http://ws.intelliPOI/”;
    private static final String URL = “http://10.0.2.2:8084/intelliPOI/GetPOIS”;

    //–//

    object = new SoapObject(NAMESPACE, METHOD_NAME);
    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

    object.addProperty(“username”, “ze”);

    object.addProperty(“latitude”, 40.211491);
    object.addProperty(“longitude”, -8.429201);
    envelope.setOutputSoapObject(object);

    HttpTransportSE aht = new HttpTransportSE(URL);
    try{

    aht.call(SOAP_ACTION, envelope);

    SoapPrimitive primitive = (SoapPrimitive) envelope.getResponse();
    tv.setText(“From Server: ” + primitive);

    }catch(Exception e){
    e.printStackTrace();
    }

    //–//

    However, I can’t get the webservice to respond.
    Can anyone help me?

  • diengsallah

    Hello,
    I want to use Android to connect to a WCF.
    Please cn you help me to resolve this problem?
    I have not any idea to start the work.
    Thank you very much

  • Fran

    Luis,

    try changing this line:
    SoapPrimitive primitive = (SoapPrimitive) envelope.getResponse();
    with:
    SoapObject soa = (SoapObject ) envelope.getResponse();

  • Manfred Moser

    Did anyone find a better solution for the https problem?

  • Manfred Moser

    Oh and a KSOAP related note. I have a patched branch that supports attribute parsing. See http://github.com/mosabua/ksoap2-android This fixes issue 17 .. http://code.google.com/p/ksoap2-android/issues/detail?id=17

  • diengsallah

    Hello,
    I am using Android to connect to a JAX-WS Web service using KSOAP2.
    My Webservice containair is Glassfish v3.
    Any time, I get this error from the server:
    org.xml.v1. XmlPullParser…
    When I use a dump, I see that the server response is that :
    Http 404
    The requested response () is not available .
    Please have you got any idea ?
    It is very difficult for me!
    Thanks

  • tobias

    Hello,

    I used your example and also tried with ksoap2 for android library.

    With your example (with the sourcecode) I get an Error with HttptransportSE

    With the android ksoap library it works. They use androidhttptransport

    But I want to use the source code like you. What can I do?

  • tobias

    Hello,

    the code works now with the sourcecode of you from ksoap2.

    But there is another problem. I can see the http request and response in wireshark.
    And I get an ERROR back in the celsius string.

    I think I now why:
    10</v:Bo

    Its because of that :n0 Namespace tag. With the special android library there is no :n0 in the tag..

    Can you give me plz a hint"

  • tobias

    sorry I`m again. It should look like this:
    5
    0260 35 3c 2f 43 65 6c 73 69 75 73 3e 3c 2f 43 65 6c 5. .

  • tobias

    ups…so here:
    5
    5. .

  • Emanuel

    Hi,
    I was following these indication, but when I ran the project I received this error “the application has stopped unexpectedly. please try again”. This error is showed specifically when I execute this line SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME) running the project step by step; any ideas?
    Thanks a lot

  • Twitty

    Hi guys I can’t import SoapObject, SoapSerializationEnvelope and HttpTransportSE!!! What more i need!

  • Piotr

    I’v got same error code as Emanuel ( “the application has stopped unexpectedly. please try again”) when I’m creating new instance of SoapObject. I’m using ksoap2 ver 2.4 form google site.
    Any ideas how to get around this problem ?

  • Axel

    THIS IS IMPORTANT!

    You must put this line on your project’s AndroidManifest file

    And this is an example of my vars:

    private static final String SOAP_ACTION = “http://tempuri.org/GetRides”;
    private static final String METHOD_NAME = “GetRides”;
    private static final String NAMESPACE = “http://tempuri.org/”;
    private static final String URL = “http://iphonedemo.stage.com/SOAP/iPhoneWS.asmx”;

    It works that way. (The urls are just an example you have to put your owns).

  • Axel

    Sory the line is:

    uses-permission android:name=”android.permission.INTERNET”

  • Nikhil

    Can there be a sample service for above client I am find diffculty in creating a webservice.

  • Gábor Auth

    Hi,

    I’ve created a new SOAP client for the Android platform, it is use a JAX-WS generated interfaces, but it is only a proof-of-concept yet.

    If you are interested, please try the example and/or watch the source: http://wiki.javaforum.hu/display/ANDROIDSOAP/Home

    :)

  • kishore

    hi guys, this is a great tutorial it works fine but, I am having problem on send xml string as request like, for authentication of user

    String myxml=”"+”string”+”"+
    “”+”string”+”"+
    “”+”318974764453″+”";.

    request.addProperty(“tagname”, myxml);

    is this the right way.

  • kishore

    tags are missing name,password,tokenid.

  • Edgar

    axel thanks for the comment that is very important

  • kishore

    hi can u suggest the best way to call web service that need to send request in xml format exactly as the below soap, is it possible in ksoap2

    the soap message is

    ?xml version=”1.0″ encoding=”utf-8″?>
    soap:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/”>
    soap:Body>
    employee xmlns=”http://abcd.com/”>
    emp>
    Name>string/Name>
    Designation>string/Designation>
    /emp>
    /employee>
    /soap:Body>
    /soap:Envelope>

    responce will be

    ?xml version=”1.0″ encoding=”utf-8″?>
    soap:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/”>
    soap:Body>
    employeeResponse xmlns=”http://abcd.com/”>
    employeeResult>string/employeeResult>
    /employeeResponse>
    /soap:Body>
    /soap:Envelope>

  • sh

    where to run the following code?

  • Milind

    Good help. thank u :)

  • Shivdatta

    Followed same as example given….giving “Conversion to Dalvik format failed with error 1″…pls help

  • hasnainkiani

    hi,
    i m newbie in android. i want to call soap web services through android .but i encourted some error when calling…..can any one help me out in this matter …..
    Error:
    04-05 00:15:29.376: WARN/System.err(627): java.lang.ClassCastException: java.lang.String
    04-05 00:15:29.406: WARN/System.err(627): at com.cmsgt.RemoteLogin.onCreate(RemoteLogin.java:67)
    04-05 00:15:29.406: WARN/System.err(627): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    04-05 00:15:29.447: WARN/System.err(627): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
    04-05 00:15:29.447: WARN/System.err(627): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
    04-05 00:15:29.447: WARN/System.err(627): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    04-05 00:15:29.447: WARN/System.err(627): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
    04-05 00:15:29.447: WARN/System.err(627): at android.os.Handler.dispatchMessage(Handler.java:99)
    04-05 00:15:29.447: WARN/System.err(627): at android.os.Looper.loop(Looper.java:123)
    04-05 00:15:29.447: WARN/System.err(627): at android.app.ActivityThread.main(ActivityThread.java:3647)
    04-05 00:15:29.465: WARN/System.err(627): at java.lang.reflect.Method.invokeNative(Native Method)
    04-05 00:15:29.465: WARN/System.err(627): at java.lang.reflect.Method.invoke(Method.java:507)
    04-05 00:15:29.465: WARN/System.err(627): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    04-05 00:15:29.465: WARN/System.err(627): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    04-05 00:15:29.465: WARN/System.err(627): at dalvik.system.NativeStart.main(Native Method)

  • hasnainkiani

    my code is:
    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.PropertyInfo;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.AndroidHttpTransport;
    //import org.ksoap2.transport.HttpTransportSE;
    import org.xmlpull.v1.XmlPullParserException;

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.Toast;

    public class RemoteLogin extends Activity {
    String METHOD_NAME1= “pollServer”;
    //String METHOD_NAME = “userLogin”; // our webservice method name
    String NAMESPACE = “http://10.0.2.2/androidserver/”; // Here package name in webservice with reverse order.
    String SOAP_ACTION = NAMESPACE + METHOD_NAME1; // NAMESPACE + method name
    final String URL = “http://10.0.2.2/androidserver/exampletest.php?wsdl”;
    // you must use ipaddress here, don’t use Hostname or localhost
    private SoapObject resultRequestSOAP = null;
    int result;
    @Override

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);

    AndroidHttpTransport androidHttpTransport =new AndroidHttpTransport(URL);
    try {
    /** Get what user typed to the EditText. */
    //String searchNameString =((EditText) findViewById(R.id.widget42)).getText().toString();
    SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME1);
    // Add the input required by web service
    PropertyInfo pi = new PropertyInfo();
    pi.setName(“value”);
    pi.setValue(1);
    pi.setType(int.class);
    request.addProperty(pi);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);
    // Make the soap call.

    try
    {
    androidHttpTransport.call(SOAP_ACTION, envelope);
    SoapObject response = (SoapObject)envelope.getResponse();
    result = Integer.parseInt(response.getProperty(0).toString());
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }

    }
    catch (Exception aE)
    {
    System.out.println(aE.toString());
    aE.printStackTrace();
    }
    }
    }

    and the server side is php code
    my server side is run properly when i m calling through php……can any one pinpoint the Error..??

  • Pingback: Android: WebService mit Android und PHP | PatSch-Work

  • jayesh

    hello huys,

    what is the myExampleHandler in the above example ? I got errro when using this example for myExampleHandler .

    can anybody tell me what is that?

    I got this error “myExampleHandler cant be resolved.”

  • Rupesh

    my code is stuck at soap object creation first line.in debug mode what to do??
    please help.I am new at android.i think object is not creating i am using ksoap2.showing some screen attach source…..