Tuesday, October 25, 2011

Android Web Service Access Tutorial


In this post I'm going to illustrate how we can access web service in Android using ksoap2-android project that provides a lightweight and efficient SOAP library for the Android platform.

You can download the jar file from following link;

http://ksoap2-android.googlecode.com/svn/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/2.5.8/ksoap2-android-assembly-2.5.8-jar-with-dependencies.jar

Here is the sample code that illustrate SOAP web service call. I have access a live web service ConvertWeight from http://www.webserviceX.NET/ which convert weight from one unit to another. I have set "envelope.dotNet = true;" in Line 53 since I'm accessing .NET web service. You can comment that line if your web service is not .NET one.

package com.sencide;
 
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
 
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
 
public class AndroidWebService extends Activity {
  
    private final String NAMESPACE = "http://www.webserviceX.NET/";
    private final String URL = "http://www.webservicex.net/ConvertWeight.asmx";
    private final String SOAP_ACTION = "http://www.webserviceX.NET/ConvertWeight";
    private final String METHOD_NAME = "ConvertWeight";
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
      
        String weight = "3700";
        String fromUnit = "Grams";
        String toUnit = "Kilograms";
      
        PropertyInfo weightProp =new PropertyInfo();
        weightProp.setName("Weight");
        weightProp.setValue(weight);
        weightProp.setType(double.class);
        request.addProperty(weightProp);
         
        PropertyInfo fromProp =new PropertyInfo();
        fromProp.setName("FromUnit");
        fromProp.setValue(fromUnit);
        fromProp.setType(String.class);
        request.addProperty(fromProp);
         
        PropertyInfo toProp =new PropertyInfo();
        toProp.setName("ToUnit");
        toProp.setValue(toUnit);
        toProp.setType(String.class);
        request.addProperty(toProp);
        
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
 
        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
            Log.i("myApp", response.toString());
    
            TextView tv = new TextView(this);
            tv.setText(weight+" "+fromUnit+" equal "+response.toString()+ " "+toUnit);
            setContentView(tv);
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

You have to add INTERNET permission to AndroidManifest.xml as follows;

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.sencide"
      android:versionCode="1"
      android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AndroidWebService"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

You can download the source code of above project (Password:sara).

I have updated my previous code in the post Android Login Screen Using HttpClient to authenticate users from web service. There I have illustrated how you can start new activity if the login is successful.

You can download the updated source code with sample .NET web service application (Password:sara).

125 comments:

  1. plz help me
    i done webservices in net and want to pass in android.i m nt understanding which webserbvices i want to use for my project.Please give reply ASAP.it is helpful for my project
    Thanks in adv

    ReplyDelete
  2. @Mamatha,
    I didn't get your question, here I have illustrate how you can call a web service using android. In your web service you can do any task according to the requirement.
    Please tell me your requirement.
    Thanks !

    ReplyDelete
  3. i have problem accessing web service in android can u help me

    ReplyDelete
  4. @siddhesh,
    Yes, please tell me your problem.

    ReplyDelete
  5. Is it possible to commucate with WCF service by these steps?

    I am not able to configure SOAP_ACTION, METHOD_NAME and other strings :(

    ReplyDelete
  6. Saranga, your sample app was just what I was looking for. I have a lot of experience with .net web services but I am new to the Android platform. I have looked at a lot of samples that are out there for KSOAP2 but none of them seemed to work as promised. Yours was straight forward and works perfectly. Thanks so much for taking the time to post this. Ken

    ReplyDelete
  7. Hi,

    Thanks for sharing this article.
    May I know what should I do with the jar file & how to do it?

    Cheers,
    Samuel

    ReplyDelete
  8. @ KRock,
    I'm so happy to hear that, Glad I could help brighten your day. :)

    ReplyDelete
  9. @ Samuel Limawan,

    First create new folder called "lib" in your project and copy the jar file to that folder.

    Then right click on your project and select Build Path --> Configure Build Path...
    Then go to Libraries tab and click "Add External Jars..." button.
    Then browse the jar file from the created "lib" file. That's it. :)

    Check my following post and there you can see screen-shots.
    Hibernate Tutorial for Beginners

    ReplyDelete
  10. Hi Saranga,

    Thank you very much for your answer.
    I have one more question :
    How to access secured asp.net webservice that requires user name & password?

    This is an example of secured method.

    _
    _.
    Public Function HelloWorld12() As String.
    If Authentication.Username = "user name" And Authentication.Password = "password" Then.
    Return "Hello World 12".
    Else.
    Return "Access Denied..."
    End If.
    End Function.

    Thanks,
    Samuel

    ReplyDelete
  11. Saranga,

    I was successful running your original sample call that called
    the web service http://www.webservicex.net/ConvertWeight.asmx. However when I try to call the same
    (basically the same) service running on my localhost I get the dreaded "expected START_TAG"... blah blah blah error.

    I saw your other sample, authenticatedUser, was running agaist localhost so I download that sample and got the same error.
    I've tried every hint I can find on the net for solving this problem

    1) I set the service to use local IIS Web Server in visual studio
    2) I tried my local IP address in leiu of localhost
    3) I tried using 10.0.2.2

    None of these work when I try and access a service running on my local server.

    How on earth did you get localhost to work?

    Ken (KRock)

    ReplyDelete
  12. @ Samuel Limawan,
    Please check the following post.
    There I have created login screen. Updated code which use web service is available at the end of this post.

    @ Ken,
    Please check following things, make sure you have define these things correctly and these are case sensitive.
    private final String NAMESPACE = "http://tempuri.org/";
    private final String URL = "http://10.0.2.2:50761/Service1.asmx?WSDL";
    final String SOAP_ACTION = "http://tempuri.org/ConvertWeight";
    final String METHOD_NAME = "ConvertWeight";
    If you are getting error please send me your code with web service project. My gmail id is saranga.rathnayake
    Thanks !

    ReplyDelete
    Replies
    1. I have the exact same error as Ken, please could you help me to know which are the fields to put in METHOD_NAME, URL, NAMESPACE and SOAP_ACTION??? My route to the webservice is this one:

      http://caracasfutbolclub.com/esite/images/mobil/webservice/Service1.asmx

      And the method that I want to read is GetNews.

      I am assuming that this is the problem for the expected Start_Tag error. Could be anything else? Thanks in advance Saranga

      Delete
  13. Saranga,

    I am going to email you the AndroidLogin sample instead of the ConvertWeight sample.
    In that one I downloaded your web service code and the android code.
    I was able to get that sample to work when I installed the web service on another machine
    running IIS on my network. You will see a comment in the web service that shows the URL
    I used that worked fine.

    The problem I have is I cannot get the same sample to work when I use my local IIS (localhost or 10.0.2.2).

    I hope you can figure this out. While working with another server is acceptable for me it would make development
    much easier and quicker if I could use localhost when testing.

    Thanks again for your help.

    Ken (KRock).

    ReplyDelete
  14. Very useful example, thank you for sharing

    ReplyDelete
  15. This is the Best Example of How Acess .Net Webservice With Android.

    Thanx Bro For Posting this example ..

    ReplyDelete
  16. @ Jimmy Alex,
    Thank you very much for your comment.

    ReplyDelete
  17. @ Piyush Angre,
    You are welcome and I'm happy if it helps you.

    ReplyDelete
    Replies
    1. can you teach me the code line by line because i dont understand the code

      Delete
  18. Saranga,
    I have same problem as kRock.
    The local web service returns 0 for addition of 2 numbers but same service hosted over the internet works properly.
    thanks.

    ReplyDelete
    Replies
    1. Please send me your code with web service, my gmail ID is saranga.rathnayake.

      Delete
  19. hello dear,
    when run the above code the emulator says application has stopped.
    please help me immediately.
    thanks bro...

    ReplyDelete
  20. hello dear,
    when run the above code the emulator says application has stopped.
    please help me immediately.
    thanks bro...

    ReplyDelete
  21. @ vineet,
    Can you please tell me what is the exception you can see in the error Log ?
    Thank You !

    ReplyDelete
  22. hello dear,
    I had remove this problem.
    Its my fault.
    thank you for your response.
    Good working and thanks for sharing this knowledge.

    ReplyDelete
  23. hi sir,
    actually i want to connect the sql server with the xml file of android application.through which i can retrive data from the sql server database and view it in xml layout.. can u pls explain how to do this with example code???
    Thank u.

    ReplyDelete
  24. Hi aiya..
    I have followed your blog post create-simple-web-service-in-visual 2010. I followed this document also. My problem is I tried to connect the web service which is in your previous tutorial(Creating a simple webservice). I configured it as follow...
    public final String NAMESPACE = "http://tempuri.org/";
    public final String URL = "http://localhost:5424/Service1.asmx";
    public final String SOAP_ACTION = "http://localhost:5424/simpleMethod";
    public final String METHOD_NAME = "simpleMethod";

    Is there any thing wrong..? Tnx in advance

    ReplyDelete
  25. Hello dear,
    How make php webservices and access through android.
    please help me with basic example.
    Thanks in advance.

    ReplyDelete
  26. Hey i need to authenticate the user with my website how to do this?

    ReplyDelete
  27. Hello Saranga. that is a very nice tutorial. and thank you for this. but i have a question. how can i access the wsdl? and it says "To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntaxL svcutil.exe https://test-service.com/Service.svc?wsdl, thank you again.

    ReplyDelete
  28. well dude its working well...
    but i want know that wsdl file generates through manually or auto generated by the server in this example u show only the rendering the data from server well but i have doubt that wsdl will generate from server side or what??? can i do the same thing for all to render the data from server.

    can u aloberate for me???

    thks in advance

    ReplyDelete
  29. This is vary useful for me thank sir

    ReplyDelete
  30. This is very useful for me for calling web Service

    ReplyDelete
  31. hi, your blog has help me a lot, but i wanted to ask you if you had a Login that uses information of a Data Bases to enter another page? thanks before hand.

    ReplyDelete
    Replies
    1. http://sarangasl.blogspot.com/2011/10/android-web-service-access-tutorial.html
      Please try above post.
      Thanks !

      Delete
  32. hi ,
    I was imported your project but it is not giving any output

    ReplyDelete
  33. Very helpful dude. Great Work!!!!!!!!!

    ReplyDelete
  34. Hi. I'm trying to access a webservice on my android app. The webservice is running in a Virtual Machine (and working, already made requests using Soap UI). I already developed my code, but i keep getting this error:

    I get exception "org.xmlpull.v1.xmlpullparserexception expected start_tag"

    i think it has to be with the SOAP_ACTION or NAMESPACE, but i already tried all the alternatives that i think that are right.
    Any help on this one plz? Thank you!

    ReplyDelete
    Replies
    1. Hi,
      First try with (envelope.dotNet = true;)
      e.g.
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
      envelope.dotNet = true;
      If it doesn't help, can you please send me the code that you are referring. My gmail id is saranga.rathnayake

      Thanks !

      Delete
  35. Hi Saranga,

    Its really good post. I appreciate for posting this, because I am new to android. But I had the following error in console while run it. Could you sort this out please..

    java.lang.NullPointerException
    at com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController.doSyncApp(AndroidLaunchController.java:923)
    at com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController.syncApp(AndroidLaunchController.java:896)
    at com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController.clientChanged(AndroidLaunchController.java:1546)

    ReplyDelete
  36. my webservice has

    attributeFormDefault="unqualified" elementFormDefault="unqualified"

    can the problem be here?

    ReplyDelete
  37. hi,
    i gt some problem with my webservice, please tell me how to write data into database through web service(WSDL) in android br means of ksoap

    ReplyDelete
  38. Hello Saranga,
    Its very useful tutorial but i am getting some error in logcat.so please suggest me.the error is under below
    java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject..
    Thanx in Advance.

    ReplyDelete
    Replies
    1. Rename the "lib" folder to "libs" Then it should work fine. Like ant android ADT looking for libs resource folder.

      Delete
  39. can you please help me with the steps or sample code for writing webservice and publishing it.....

    ReplyDelete
  40. Hi
    i need your help
    i connect web service .net protocol (https)
    i getting value "javax.net.ssl.SSLException:Not trusted server certificate"

    ReplyDelete
  41. Hi saranga, this is nag , please tell me how to retrieve the data from web server into our application?
    How to retrieve some feilds from websites?

    ReplyDelete
  42. thanks a lot dear...
    ur example helps me a lot but there is a problem when we call web service method with parameters, response returns null but without parameter method returns value. Kindly help me...

    ReplyDelete
    Replies
    1. Dear sanchitsoftware,

      i have the same probleme, did you find the solution of this issue?

      best regards

      Delete
  43. Hi Saranga, I am following ur tutorial but I am getting java.net.ConnectionException error.. ll u plz help me..

    ReplyDelete
  44. Hi...
    Pls tell me how to store data on web server using asp.net webservice in android...

    ReplyDelete
  45. hi,im trying to run this tutorial but the response is not displayed in android 4.0.3 or even 2.3.3,pliz help

    ReplyDelete
  46. HI Saranga

    Thank you veey very much

    Great tutorial!

    ReplyDelete
  47. Thank you Sir
    Great tutorial!

    ReplyDelete
  48. Saranga Rathnayaka hi saranga i am new to android development could you tell me career in android as i am from the background of .net at the same time could you tell me my career at +2years

    ReplyDelete
  49. here we are consuming asmx service but i need to consume svc service
    can you help me??

    ReplyDelete
  50. THIS TUTORIAL is great.I tried to use it for another service but it returns false...internet permissions are set.what could be the problem. this particular one runs

    ReplyDelete
  51. THIS TUTORIAL is great.I tried to use it for another service but it returns false...internet permissions are set.what could be the problem. this particular one runs

    ReplyDelete
  52. thank u very much every day i visit u r blog nice work thank u very muvh

    ReplyDelete
  53. thank u so much gud tutorial i am every day seeing this very gud work helps me so much thanks thanks:):)

    ReplyDelete
  54. what will be the password and username to enter successfully through next activity??

    ReplyDelete
  55. when run the above code the emulator says application has stopped.
    and i dont know how to solve this problem.. please advice as fast as possible

    ReplyDelete
  56. when i run the code the emulator says application has stopped.
    i dont know how to solve this problem

    please advice as fast as possible coz i need to fix this problem

    ReplyDelete
  57. Hi Saranga,

    Thank you so much for this post.

    With some other config, I can access my own web service(using SOAP and running on local host of PC) from my Android device with URL "http://ip-address/mywebservice".

    However, if I create a RESTful web service, I can browser it with url "http://localhost/mywebservice" but cant browser with "http://ip-address/mywebservice". And, of course, I can't access this web service from my Android device.

    Do you have any information to help me?
    Hope to hear you soon.

    Best regards,
    Dzung

    ReplyDelete
  58. Hi Saranga,

    Thank you so much for this post.

    With some other configs, I can access to my own SOAP web service (running on local host of PC) from my Android device with URL: http://ip-address/mywebservice.

    However, if I create a RESTful webservice, I can browse it as http://localhost/mywebservice but can't browse with http://ip-address/mywebservice. And, of course, I can't access it from my Android device.

    Do you have any information to help me?
    I hope to hear you soon.

    Thank you so much.
    Dz

    ReplyDelete
  59. @Saranga Rathnayake
    I am used your code in my eclipse,
    but it did not showed any response on my emulator screen,
    is that problem with my eclipse or what,
    i have not installed WTP plugin,
    does that cause this,
    plz help.
    i have tried to debug it but it also does not help,
    and i got warning that said
    "This element neither has attached source nor attached Javadoc and hence no Javadoc could be found.".

    ReplyDelete
  60. Hello sir i am Rajan.

    I am trying to fetch the data from the .asmx web service since last 1 or 2 days but i didn't get any proper output.

    I have also try your example but i am getting the same exception, i didn't understand what is going on. so, sir please help me as soon as possible, here i am putting my logcat entry...

    Logcat
    ------
    W/System.err(880): org.xmlpull.v1.XmlPullParserException: unterminated entity ref (position:TEXT ? ? in java.io.InputStreamReader@44ef9a90)

    08-06 15:06:46.741: W/System.err(880): at org.kxml2.io.KXmlParser.exception(KXmlParser.java:273)
    08-06 15:06:46.751: W/System.err(880): at org.kxml2.io.KXmlParser.error(KXmlParser.java:269)
    08-06 15:06:46.751: W/System.err(880): at org.kxml2.io.KXmlParser.pushEntity(KXmlParser.java:787)
    08-06 15:06:46.751: W/System.err(880): at org.kxml2.io.KXmlParser.pushText(KXmlParser.java:855)
    08-06 15:06:46.751: W/System.err(880): at org.kxml2.io.KXmlParser.nextImpl(KXmlParser.java:354)
    08-06 15:06:46.751: W/System.err(880): at org.kxml2.io.KXmlParser.next(KXmlParser.java:1385)
    08-06 15:06:46.751: W/System.err(880): at org.kxml2.io.KXmlParser.nextTag(KXmlParser.java:1415)
    08-06 15:06:46.751: W/System.err(880): at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:127)
    08-06 15:06:46.751: W/System.err(880): at org.ksoap2.transport.Transport.parseResponse(Transport.java:100)
    08-06 15:06:46.751: W/System.err(880): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:214)
    08-06 15:06:46.761: W/System.err(880): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:96)
    08-06 15:06:46.761: W/System.err(880): at com.simform.androidsoapwebservicedemo.MainActivity.onCreate(MainActivity.java:58)
    08-06 15:06:46.761: W/System.err(880): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    08-06 15:06:46.761: W/System.err(880): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    08-06 15:06:46.761: W/System.err(880): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    08-06 15:06:46.761: W/System.err(880): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    08-06 15:06:46.761: W/System.err(880): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    08-06 15:06:46.761: W/System.err(880): at android.os.Handler.dispatchMessage(Handler.java:99)
    08-06 15:06:46.761: W/System.err(880): at android.os.Looper.loop(Looper.java:123)
    08-06 15:06:46.761: W/System.err(880): at android.app.ActivityThread.main(ActivityThread.java:4627)
    08-06 15:06:46.761: W/System.err(880): at java.lang.reflect.Method.invokeNative(Native Method)
    08-06 15:06:46.761: W/System.err(880): at java.lang.reflect.Method.invoke(Method.java:521)
    08-06 15:06:46.761: W/System.err(880): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    08-06 15:06:46.761: W/System.err(880): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    08-06 15:06:46.761: W/System.err(880): at dalvik.system.NativeStart.main(Native Method)

    ReplyDelete
    Replies
    1. Hi saranga,

      Hi tried your code but I am getting a run time exception that application stopped unexpectedly.

      can you please help me.

      please find the logcat below.


      08-10 08:18:50.696: E/AndroidRuntime(490): FATAL EXCEPTION: main
      08-10 08:18:50.696: E/AndroidRuntime(490): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
      08-10 08:18:50.696: E/AndroidRuntime(490): at com.example.Web_Services.Web_ServicesActivity.onCreate(Web_ServicesActivity.java:28)
      08-10 08:18:50.696: E/AndroidRuntime(490): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
      08-10 08:18:50.696: E/AndroidRuntime(490): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
      08-10 08:18:50.696: E/AndroidRuntime(490): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
      08-10 08:18:50.696: E/AndroidRuntime(490): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
      08-10 08:18:50.696: E/AndroidRuntime(490): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
      08-10 08:18:50.696: E/AndroidRuntime(490): at android.os.Handler.dispatchMessage(Handler.java:99)
      08-10 08:18:50.696: E/AndroidRuntime(490): at android.os.Looper.loop(Looper.java:123)
      08-10 08:18:50.696: E/AndroidRuntime(490): at android.app.ActivityThread.main(ActivityThread.java:3683)
      08-10 08:18:50.696: E/AndroidRuntime(490): at java.lang.reflect.Method.invokeNative(Native Method)
      08-10 08:18:50.696: E/AndroidRuntime(490): at java.lang.reflect.Method.invoke(Method.java:507)
      08-10 08:18:50.696: E/AndroidRuntime(490): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
      08-10 08:18:50.696: E/AndroidRuntime(490): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
      08-10 08:18:50.696: E/AndroidRuntime(490): at dalvik.system.NativeStart.main(Native Method)



      Regards,
      sandesh(sandeshk779@gmail.com)

      Delete
    2. finally i got the solution after following this blog


      http://lalit3686.blogspot.in/2012/06/calling-soap-webservice-using-httppost.html

      Delete
  61. Hi Saranga,

    I have been trying to learn about calling web services from android and found your tutorial online.
    I have imported it and getting the following error for each of the methods below:

    weightProp.setName("Weight");
    weightProp.setValue(weight);
    weightProp.setType(double.class);
    request.addProperty(weightProp);



    "The method setName(String) is undefined for the type weightProp"
    Is there something missing? Do I need to have an extra class, or how do I define these methods?

    Could you please please help me??

    Thank you soo much.

    ReplyDelete
  62. Assalamulaikum Saranga,
    You have helped me alot with this post. May Allah bless you.

    ReplyDelete
  63. This is very nice tutorial...thanks for posting this tutorial....how is update dropdown value from android to mysql database....please help me or post the tutorial for this.

    ReplyDelete
  64. Nice Blog !

    However, can you please also let us know how to access a WebService which requires Username/Password Authentication ?

    ReplyDelete
  65. It worked for me.
    Thanks a lot :)

    ReplyDelete
  66. Hi, i check above code in my eclipse.But i got response result value=0, so please help me how to get exact value

    ReplyDelete
  67. please tell me why it is not run on android 2.2 onwards version

    ReplyDelete
    Replies
    1. will i use json to access the web services in .net.if yes tell me the answer code.

      Delete
  68. how can i send two textbox data to my Mysql database i.e. localhost(wamp)

    ReplyDelete
  69. Hello. I downloaded your code and tried it in both emulator and mobile but it force closes on both platforms. Perhaps you can help? I am trying to run your code in eclipse and android version 2.2 onwards.

    Thank you

    ReplyDelete
  70. Saurin
    One solution might be to rename the "lib" folder in the original uploaded project to "libs". It worked for me to get rid of the original crashings

    ReplyDelete
  71. Saranga, I have a question. I was able to make the application run, then I run the web service, I checked on the method in the .NET project, and is comparing user and pass with user=saranga, and pass=saranga. I type that on the Android app but get always "LOGIN FAILED" message. Any idea what could be causing this?
    Thanks
    Miguel

    ReplyDelete
  72. Hai Saranga,
    When i tried to execute code what u written it gives the blank screen and line
    androidHttpTransport.call(SOAP_ACTION, envelope); doesn't reach, I am Using Android 4.0.
    please help me

    ReplyDelete
  73. Hello Saranga
    Same for me, please help
    Thanks

    ReplyDelete
  74. Can any one help me...
    I have tried Web service for 1st time and deployed it on local server its url is http://172.18.6.177:8080/TrialApp/Login?wsdl
    and method name is authenticate

    What will me NAMESPACE and SOAP_ACTION

    ReplyDelete
  75. hai Saranja
    i tried u r code but geting error in the log
    the error (java.net.UnknownHostException: Host is unresolved: www.webservicex.net:80)
    pls help me

    ReplyDelete
  76. one of the best example i have ever come across thanks dude!!perfectly working

    ReplyDelete
  77. hi nice tutorial...
    it really helpful for me.

    ReplyDelete
  78. Hello Saranga
    your post is very useful, but I have a small problem
    I am new to Android platform.

    My web service is accessible via a web application. Net.
    but I can not get a response via Andriod.
    When I call 'androidHttpTransport.call (Exec_SOAP_ACTION, envelope);'
    execution proceeds directly into the 'catch'

    have you an idea about the origin to the problem.
    Thx ...

    ReplyDelete
  79. Hello Saranga,
    your post is very useful, but I have a small problem
    I am new to Android platform.

    My web service is accessible via a web application. Net.
    but I can not get a response via Andriod.
    When I call the 'androidHttpTransport.call (My_SOAP_ACTION, envelope);'
    execution proceeds directly into the 'catch' and (Exception e) is null
    have you any idea about the origin of the problem.
    Thx ...

    ReplyDelete
  80. Bro, It's great, working fine,
    But it's not the one, which i exactly need,
    I get the "false" loop when i enter the username and password in the fields off android... But i have the same fed already in the database!!It shows the echo the successful login, but.... if (str.toString().equalsIgnoreCase("true"))fails! Any idea???

    ReplyDelete
  81. Hello Dear ,
    i Make it as same u have done.
    I ran it on AVD but no error and no result is showing..

    ReplyDelete
  82. In above example i have done same as u shown in example. but no result and no error is showing.

    ReplyDelete
  83. This is very useful information.. thank you..
    I need your help
    I have my webservice running but i need to call it many times,it is taking the parameter only which i assigned for the first time,its not getting changed in propertyinfo

    ReplyDelete
  84. i need to call a webservice repeatedly in my android application
    so how do i do this

    ReplyDelete
  85. Hi. I hope you can help me with this. The program stops when I reach the line with: SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    any idea? Thanks in advantage

    ReplyDelete
  86. Getting a error as follows: Please help me to resolve the same

    11-01 16:38:53.929: E/AndroidRuntime(18212): FATAL EXCEPTION: main.
    11-01 16:38:53.929: E/AndroidRuntime(18212): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject.
    11-01 16:38:53.929: E/AndroidRuntime(18212): at com.sencide.AndroidWebService.onCreate(AndroidWebService.java:28).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at android.app.ActivityThread.access$1500(ActivityThread.java:117).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at android.os.Handler.dispatchMessage(Handler.java:99).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at android.os.Looper.loop(Looper.java:130).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at android.app.ActivityThread.main(ActivityThread.java:3687).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at java.lang.reflect.Method.invokeNative(Native Method).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at java.lang.reflect.Method.invoke(Method.java:507).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at dalvik.system.NativeStart.main(Native Method).

    ReplyDelete
  87. Please help me on the above error

    11-01 16:38:53.929: E/AndroidRuntime(18212): FATAL EXCEPTION: main.
    11-01 16:38:53.929: E/AndroidRuntime(18212): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject.
    11-01 16:38:53.929: E/AndroidRuntime(18212): at com.sencide.AndroidWebService.onCreate(AndroidWebService.java:28).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at android.app.ActivityThread.access$1500(ActivityThread.java:117).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at android.os.Handler.dispatchMessage(Handler.java:99).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at android.os.Looper.loop(Looper.java:130).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at android.app.ActivityThread.main(ActivityThread.java:3687).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at java.lang.reflect.Method.invokeNative(Native Method).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at java.lang.reflect.Method.invoke(Method.java:507).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625).
    11-01 16:38:53.929: E/AndroidRuntime(18212): at dalvik.system.NativeStart.main(Native Method).

    ReplyDelete
  88. Hi am using all these code but not any result comes on my layout scree it's a blank screen

    ReplyDelete
  89. Hi All,

    I am new to android programming, can you help on importing this jar file on android studio?

    Thank you in advance.

    ReplyDelete
  90. Hello Brother I don't know about web services and parsing.

    can u please share a simple program to understand both

    ReplyDelete
  91. hiii brother,

    this code does not work in my project.
    its does not call
    androidHttpTransport.call(SOAP_ACTION, envelope);
    and Null Pointer exception is generate.
    Please help me....
    Thank you

    ReplyDelete
  92. Can you please tell me the username & password? When i entered login, it will show as unfortunately AndroidLogin has stopped

    ReplyDelete
  93. i am new to android when i tryed to login using your code but nothig happens i am using asp.net webservice i tryed to find the problem i felt it at
    HttpResponse response = httpclient.execute(httppost);
    will you help

    ReplyDelete
  94. code not working something wrong at
    HttpResponse response = httpclient.execute(httppost);
    will someone help

    ReplyDelete
  95. Hi
    Plz post page on consuming httpstransportse ksoap2 object connection for WCF services.

    Thanks in advance

    ReplyDelete
  96. Any idea , i have problem duing passing object: http://stackoverflow.com/questions/23364562/passing-object-on-soap-and-getting-java-lang-runtimeexception-cannot-serialize

    ReplyDelete
  97. Guys I have a problem during passing object. http://stackoverflow.com/questions/23364562/passing-object-on-soap-and-getting-java-lang-runtimeexception-cannot-serialize

    ReplyDelete
  98. SoapPrimitive response = (SoapPrimitive)envelope.getResponse(); i have to read a particulor
    child but your code is response is show all json type xml so i have to read a particular one Single child node plz reply fast

    ReplyDelete
  99. Good post , Have you any idea about How to implement same example what you have given above , over the ssl . please share it. Thanks

    ReplyDelete
  100. Good post. ..... Have you any idea about how to implement the same example what you have given , over the SSL ..... Please share it. Thanks .....
    Happy coding ;)

    ReplyDelete
  101. Hi,Saranga Rathnayake
    I have small requrement, I was dovelop a barcode scaner app.
    it's work properly and it give barcode nuber.Now my requrement is ''WRITE A WEB SERVICE TO take that barcode number and DISPLAY THE PRODUCT DETAILS OF THT barcode number. I have probem with write a web service code,


    please help me to compleate this application

    ReplyDelete
  102. Hi there, I hv tried running your code, but I got the error. When I debug, the error is from here.
    androidHttpTransport.call(SOAP_ACTION, envelope);

    Error i am getting is:android.os.NetworkOnMainThreadException

    can help on this? I gave internet permission in manifest file also.

    Any help is very much appreciated. Thank you in advance

    ReplyDelete
  103. i face error here
    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.PropertyInfo;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapPrimitive;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;

    ReplyDelete