{"id":1502,"date":"2011-01-27T21:03:00","date_gmt":"2011-01-27T15:33:00","guid":{"rendered":"http:\/\/naveenbalani.com\/?p=1502"},"modified":"2017-03-24T17:52:53","modified_gmt":"2017-03-24T12:22:53","slug":"invoke-webservices-from-android","status":"publish","type":"post","link":"https:\/\/navveenbalani.dev\/index.php\/articles\/invoke-webservices-from-android\/","title":{"rendered":"Invoke webservices from Android"},"content":{"rendered":"<p>This is ongoing blog on <a href=\"..\/index.php\/tag\/android\/\">Getting Started with Android<\/a>. In <a href=\"..\/index.php\/2011\/01\/getting-started-with-android\/\">earlier blog<\/a>, I provided an architecture overview of android application, followed by setting up the development environment for Android and creating and running a simple application.<\/p>\n<p>In this blog, I would describe how to invoke web services (soap based services) via Android. In my next blog, I would follow it up with how to invoke REST based services. For trying out the tutorial, you need to have the android development environment setup as mentioned in my <a href=\"http:\/\/naveenbalani.com\/index.php\/2011\/01\/getting-started-with-android-creating-android-application\/\">previous blog<\/a>.<\/p>\n<p>There are two ways in which invoke web services<\/p>\n<ul>\n<li>Raw APIs\u00a0 : Use the HttpClient and XML parser to manually create a soap request and parse the soap response.<\/li>\n<\/ul>\n<ul>\n<li>Using a soap client library : like KSOAP library which does the low level work for parsing and dealing with soap messages \u2013 For Android, there is library available at <a href=\"http:\/\/code.google.com\/p\/ksoap2-android\/\">http:\/\/code.google.com\/p\/ksoap2-android\/<\/a> . Its good to see some active development for KSOAP 2, I remembered I wrote the first article on KSOAP 2 way back in 2003 ( <a href=\"http:\/\/naveenbalani.com\/index.php\/2010\/05\/deliver-web-services-to-mobiles\/\">http:\/\/naveenbalani.com\/index.php\/2010\/05\/deliver-web-services-to-mobiles\/<\/a>)and good to see it back in development for android.<\/li>\n<\/ul>\n<p>I would start development with the later approach, but I plan to use RAW APIs in the follow up post &#8211;<\/p>\n<p>Download the KSOAP2 library , go to <a href=\"http:\/\/code.google.com\/p\/ksoap2-android\/\">http:\/\/code.google.com\/p\/ksoap2-android\/<\/a> , click on downloads link on the menu, and download the latest release artifact \u2013 <a href=\"http:\/\/code.google.com\/p\/ksoap2-android\/source\/browse\/m2-repo\/com\/google\/code\/ksoap2-android\/ksoap2-android-assembly\/2.5.2\/ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar\">http:\/\/code.google.com\/p\/ksoap2-android\/source\/browse\/m2-repo\/com\/google\/code\/ksoap2-android\/ksoap2-android-assembly\/2.5.2\/ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar<\/a> . In the release artifact page, click on &#8220;View raw file&#8221; and select &#8220;Save Link as&#8221; and download the jar file which has all the required dependencies.<\/p>\n<p>Next we would create a sample android project which would invoke a .NET web service. I decided to host a simple .NET web service in my website , so it would easier for you all to try out the sample . The web service is available at <a href=\"http:\/\/naveenbalani.com\/WassupAndroid.asmx\">http:\/\/naveenbalani.com\/WassupAndroid.asmx<\/a><\/p>\n<p>This is a simple .NET service, with one operation called todayMessage(), which display \u201cWassup Android from a .NET application \u201c as output.<\/p>\n<p>To create an andrioid project.\u00a0 Start eclipse.<\/p>\n<ul>\n<li>Select File &gt; New &gt; Project.<\/li>\n<li>Select Android &gt; Android Project, Click Next.<\/li>\n<li>Enter the following information for the project &#8211;<\/li>\n<\/ul>\n<p style=\"padding-left: 60px;\">Project name &#8211; \u00a0AndroidClientService<\/p>\n<p style=\"padding-left: 60px;\">Build Target &#8211; Android 2.3<\/p>\n<p style=\"padding-left: 60px;\">Application name &#8211; \u00a0WasuppTodaysMessage<\/p>\n<p style=\"padding-left: 60px;\">Package name &#8211; org.android.websevice.client.samples<\/p>\n<p style=\"padding-left: 60px;\">Create Activity &#8211; \u00a0AndroidClientService<\/p>\n<p style=\"padding-left: 60px;\">Min SDK Version &#8211; \u00a09<\/p>\n<ul>\n<li>Click Finish<\/li>\n<\/ul>\n<p>This would create a Project called AndroidClientService in your workspace.<\/p>\n<p>Next , add the ksoap2-andriod dependency to the project. Select the AndroidClientService, click properties , click on Java build path , click on Libraries , select Add External Jars and add the ksoap2 library (ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar) and click Ok.<\/p>\n<p>Next, open up the WasuppServiceClientAndroid class and replace the onCreate method with the following onCreate() method as shown in listing below. Following shows the complete code listing.<\/p>\n<p>This project would invoke the web service and display \u2013 \u201c \u201c \u00a0on the device when the application is executed. To build the project, select Project -&gt; Clean<\/p>\n<pre>\r\npackage android.websevice.client.samples;\r\n\r\nimport org.ksoap2.SoapEnvelope;\r\nimport org.ksoap2.serialization.SoapObject;\r\nimport org.ksoap2.serialization.SoapSerializationEnvelope;\r\nimport org.ksoap2.transport.HttpTransportSE;\r\n\r\nimport android.app.Activity;\r\nimport android.os.Bundle;\r\nimport android.widget.TextView;\r\n\r\npublic class AndroidClientService extends Activity {\r\n\r\nprivate static final String SOAP_ACTION = \"http:\/\/www.naveenbalani.com\/webservices\/WassupAndroidService\/todaysMessage\";\r\n\r\nprivate static final String OPERATION_NAME = \"todaysMessage\";\r\n\r\nprivate static final String WSDL_TARGET_NAMESPACE = \"http:\/\/www.naveenbalani.com\/webservices\/WassupAndroidService\/\";\r\n\r\nprivate static final String SOAP_ADDRESS = \"http:\/\/naveenbalani.com\/WassupAndroid.asmx\";\r\n\r\n@Override\r\npublic void onCreate(Bundle savedInstanceState) {\r\nsuper.onCreate(savedInstanceState);\r\n\r\nTextView textView = new TextView(this);\r\n\r\nsetContentView(textView);\r\n\r\nSoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,\r\nOPERATION_NAME);\r\n\r\nSoapSerializationEnvelope envelope = new SoapSerializationEnvelope(\r\nSoapEnvelope.VER11);\r\nenvelope.dotNet = true;\r\n\r\nenvelope.setOutputSoapObject(request);\r\n\r\nHttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);\r\n\r\ntry\r\n\r\n{\r\n\r\nhttpTransport.call(SOAP_ACTION, envelope);\r\n\r\nObject response = envelope.getResponse();\r\n\r\ntextView.setText(response.toString());\r\n\r\n}\r\n\r\ncatch (Exception exception)\r\n\r\n{\r\n\r\ntextView.setText(exception.toString());\r\n\r\n}\r\n\r\n}\r\n}\r\n<\/pre>\n<p>To run the AndroidClientService \u00a0Android application, click on it and select Run As &gt; Android Application.<\/p>\n<p>On the eclipse console, you would see the following similar message \u2013<\/p>\n<p>[AndroidClientService] Performing android.websevice.client.samples.AndroidClientService activity launch<\/p>\n<p>[AndroidClientService] Automatic Target Mode: launching new emulator with compatible AVD &#8216;AVD&#8217;<\/p>\n<p>[AndroidClientService] Launching a new emulator with Virtual Device &#8216;AVD&#8217;<\/p>\n<p>[AndroidClientService] Waiting for HOME (&#8216;android.process.acore&#8217;) to be launched&#8230;<\/p>\n<p>You should see the Android AVD being launched. After the above message, it takes a while (2-3 minutes) for the first time to get the Android home page on the emulator.<\/p>\n<p>After the device is started, you should see the following message on console..<\/p>\n<p>[AndroidClientService] Uploading AndroidClientService.apk onto device &#8217;emulator-5554&#8242;<\/p>\n<p>[AndroidClientService] Installing AndroidClientService.apk&#8230;<\/p>\n<p>[AndroidClientService] Success!<\/p>\n<p>[AndroidClientService] Starting activity android.websevice.client.samples.AndroidClientService on device emulator-5554<\/p>\n<p>If the application doesn\u2019t show up on the emulator, Click on Menu option on the emulator and you would see the WasuppTodayMessage android application and message being displayed.<\/p>\n<p><a href=\"http:\/\/naveenbalani.com\/wp-content\/uploads\/2011\/01\/WebService-AndroidWebServiceOutput1.jpg\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1504\" title=\"WebService-AndroidWebServiceOutput\" src=\"http:\/\/naveenbalani.com\/wp-content\/uploads\/2011\/01\/WebService-AndroidWebServiceOutput1.jpg\" alt=\"\" width=\"300\" height=\"213\" \/><\/a><\/p>\n<p>Issues encountered during invoking the web services application from Android Emulator<\/p>\n<ul>\n<li>Unknown host exception\u00a0 \u2013<\/li>\n<\/ul>\n<p>If you get the following exception &#8211; \u201cjava.net.UnKnownHostException: naveenbalani.com\u2019, than you need to add required domain name server which emulator would use to resolve domain.<\/p>\n<p>A list of network limitations on emulator is available at &#8211; <a href=\"http:\/\/developer.android.com\/guide\/developing\/tools\/emulator.html#networkinglimitations\">http:\/\/developer.android.com\/guide\/developing\/tools\/emulator.html#networkinglimitations<\/a><\/p>\n<p>As per the documentation \u2013 \u201c<\/p>\n<p>&#8220;At startup, the emulator reads the list of DNS servers that your system is currently using. It then stores the IP addresses of up to four servers on this<br \/>\nlist and sets up aliases to them on the emulated addresses 10.0.2.3, 10.0.2.4, 10.0.2.5 and 10.0.2.6 as needed.<\/p>\n<p>On Linux and OS X, the emulator obtains the DNS server addresses by parsing the file \/etc\/resolv.conf. On Windows, the emulator obtains the addresses by calling the GetNetworkParams() API. Note that this usually means that the emulator ignores the content of your &#8220;hosts file&#8221;<\/p>\n<p>Now, to add the domain name server, click on Run configurations and select AndroidClientService and add the following<\/p>\n<p>-dns-server ns15.unitechost.in<\/p>\n<p>in the additional emulator command line options as shown below. Click Run to run the configuration<\/p>\n<p><a href=\"http:\/\/naveenbalani.com\/wp-content\/uploads\/2011\/01\/AndroidDNS.jpg\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1505\" title=\"AndroidDNS\" src=\"http:\/\/naveenbalani.com\/wp-content\/uploads\/2011\/01\/AndroidDNS.jpg\" alt=\"\" width=\"300\" height=\"264\" \/><\/a><\/p>\n<ul>\n<li>Security<\/li>\n<\/ul>\n<p>If you get a permission issue while accessing internet, you need to add the following line in &lt;uses-permission android:name=&#8221;android.permission.INTERNET&#8221;&gt;&lt;\/uses-permission&gt; to allow application to access \u00a0internet<\/p>\n<p>Here is the\u00a0 complete listing of AndroidManifest.xml<\/p>\n<pre>\r\n&amp;lt;?xml version=\"1.0\" encoding=\"utf-8\"?&amp;gt;\r\n&amp;lt;manifest xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n&lt;%%KEEPWHITESPACE%%&gt; package=\"android.websevice.client.samples\"\r\n&lt;%%KEEPWHITESPACE%%&gt; android:versionCode=\"1\"\r\n&lt;%%KEEPWHITESPACE%%&gt; android:versionName=\"1.0\"&amp;gt;\r\n&lt;%%KEEPWHITESPACE%%&gt; &amp;lt;application android:icon=\"@drawable\/icon\" android:label=\"@string\/app_name\"&amp;gt;\r\n&lt;%%KEEPWHITESPACE%%&gt; &amp;lt;activity android:name=\".AndroidClientService\"\r\n&lt;%%KEEPWHITESPACE%%&gt; android:label=\"@string\/app_name\"&amp;gt;\r\n&lt;%%KEEPWHITESPACE%%&gt; &amp;lt;intent-filter&amp;gt;\r\n&lt;%%KEEPWHITESPACE%%&gt; &amp;lt;action android:name=\"android.intent.action.MAIN\" \/&amp;gt;\r\n&lt;%%KEEPWHITESPACE%%&gt; &amp;lt;category android:name=\"android.intent.category.LAUNCHER\" \/&amp;gt;\r\n&lt;%%KEEPWHITESPACE%%&gt; &amp;lt;\/intent-filter&amp;gt;\r\n&lt;%%KEEPWHITESPACE%%&gt; &amp;lt;\/activity&amp;gt;\r\n\r\n&lt;%%KEEPWHITESPACE%%&gt; &amp;lt;\/application&amp;gt;\r\n&lt;%%KEEPWHITESPACE%%&gt; &amp;lt;uses-sdk android:minSdkVersion=\"9\" \/&amp;gt;\r\n&amp;lt;uses-permission android:name=\"android.permission.INTERNET\"&amp;gt;&amp;lt;\/uses-permission&amp;gt;\r\n&amp;lt;\/manifest&amp;gt;\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is ongoing blog on Getting Started with Android. In earlier blog, I provided an architecture overview of android application, followed by setting up the development environment for Android and creating and running a simple application. In this blog, I would describe how to invoke web services (soap based services) via Android. In my next [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1504,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[149,3,10,80],"tags":[145,260],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.0.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Invoke webservices from Android - Current and Future Technology Trends by Navveen Balani<\/title>\n<meta name=\"description\" content=\"Invoke webservices from Android -\" \/>\n<link rel=\"canonical\" href=\"https:\/\/navveenbalani.dev\/index.php\/articles\/invoke-webservices-from-android\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Invoke webservices from Android - Current and Future Technology Trends by Navveen Balani\" \/>\n<meta property=\"og:description\" content=\"Invoke webservices from Android -\" \/>\n<meta property=\"og:url\" content=\"https:\/\/navveenbalani.dev\/index.php\/articles\/invoke-webservices-from-android\/\" \/>\n<meta property=\"og:site_name\" content=\"Current and Future Technology Trends by Navveen Balani\" \/>\n<meta property=\"article:published_time\" content=\"2011-01-27T15:33:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-03-24T12:22:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2011\/01\/WebService-AndroidWebServiceOutput1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"213\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"6 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/navveenbalani.dev\/#website\",\"url\":\"https:\/\/navveenbalani.dev\/\",\"name\":\"Current and Future Technology Trends by Navveen Balani\",\"description\":\"Current and Future Technology Trends by Navveen Balani\",\"publisher\":{\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/navveenbalani.dev\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/invoke-webservices-from-android\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2011\/01\/WebService-AndroidWebServiceOutput1.jpg\",\"width\":\"300\",\"height\":\"213\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/invoke-webservices-from-android\/#webpage\",\"url\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/invoke-webservices-from-android\/\",\"name\":\"Invoke webservices from Android - Current and Future Technology Trends by Navveen Balani\",\"isPartOf\":{\"@id\":\"https:\/\/navveenbalani.dev\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/invoke-webservices-from-android\/#primaryimage\"},\"datePublished\":\"2011-01-27T15:33:00+00:00\",\"dateModified\":\"2017-03-24T12:22:53+00:00\",\"description\":\"Invoke webservices from Android -\",\"breadcrumb\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/invoke-webservices-from-android\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/navveenbalani.dev\/index.php\/articles\/invoke-webservices-from-android\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/invoke-webservices-from-android\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/navveenbalani.dev\/\",\"url\":\"https:\/\/navveenbalani.dev\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/invoke-webservices-from-android\/\",\"url\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/invoke-webservices-from-android\/\",\"name\":\"Invoke webservices from Android\"}}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/invoke-webservices-from-android\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/invoke-webservices-from-android\/#webpage\"},\"author\":{\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\"},\"headline\":\"Invoke webservices from Android\",\"datePublished\":\"2011-01-27T15:33:00+00:00\",\"dateModified\":\"2017-03-24T12:22:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/invoke-webservices-from-android\/#webpage\"},\"commentCount\":10,\"publisher\":{\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\"},\"image\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/invoke-webservices-from-android\/#primaryimage\"},\"keywords\":\"android,Mobile Computing\",\"articleSection\":\"Android,Articles,Featured,Mobile Computing\",\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\",\"name\":\"Navveen\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/navveenbalani.dev\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2019\/07\/navveen_balani.jpeg\",\"width\":200,\"height\":200,\"caption\":\"Navveen\"},\"logo\":{\"@id\":\"https:\/\/navveenbalani.dev\/#personlogo\"},\"sameAs\":[\"http:\/\/naveenbalani.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts\/1502"}],"collection":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/comments?post=1502"}],"version-history":[{"count":14,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts\/1502\/revisions"}],"predecessor-version":[{"id":2380,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts\/1502\/revisions\/2380"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/media\/1504"}],"wp:attachment":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/media?parent=1502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/categories?post=1502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/tags?post=1502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}