Accessing data from a Microsoft Web Service

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Accessing data from a Microsoft Web Service

Postby CharlesBrauer » Mon Mar 01, 2010 11:12 pm

I have accessed my test Web Service with the Android 2.1 simulator. I am using the latest Eclipse and Java systems to build the Android application.

Here is what I did to make it work:

1. My Web Service that was built by Visual Studio 2008 and .Net 3.5.

The Web Service URL is:
http://www.cypresspoint.com/DroidServic ... rvice.asmx.
This address is live. Please try it in your browser.

2. To make my web site compatible with Android, I had to add:
<webServices>
<protocols>
<add name="HttpSoap"/>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
.
.
.
To the Visual Studio 2008 WebConfig.xml file.

3. I have downloaded ksoap2-android from:
http://code.google.com/p/ksoap2-android/.

4. I installed the jar-file using the instructions at:
http://developer.android.com/guide/appe ... nallibrary.

My Android test code is:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.hedgetools.testwebservice;
  3.  
  4.  
  5.  
  6. import org.ksoap2.SoapEnvelope;
  7.  
  8. import org.ksoap2.serialization.SoapObject;
  9.  
  10. import org.ksoap2.serialization.SoapPrimitive;
  11.  
  12. import org.ksoap2.serialization.SoapSerializationEnvelope;
  13.  
  14. import org.ksoap2.transport.AndroidHttpTransport;
  15.  
  16.  
  17.  
  18. import android.app.*;
  19.  
  20. import android.os.*;
  21.  
  22. import android.widget.TextView;
  23.  
  24.  
  25.  
  26. public class TestWebService extends Activity {
  27.  
  28.   private static final String SOAP_ACTION = "http://www.CypressPoint.com/DroidService/HelloWorld";
  29.  
  30.   private static final String METHOD_NAME = "HelloWorld";
  31.  
  32.   private static final String NAMESPACE   = "http://www.CypressPoint.com/DroidService/";
  33.  
  34.   private static final String URL = "http://www.CypressPoint.com/DroidService/DroidWebService.asmx";
  35.  
  36.  
  37.  
  38.   // private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
  39.  
  40.   // private static final String METHOD_NAME = "CelsiusToFahrenheit";
  41.  
  42.   // private static final String NAMESPACE   = "http://tempuri.org/";
  43.  
  44.   // private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
  45.  
  46.  
  47.  
  48.   TextView tv;
  49.  
  50.  
  51.  
  52.   @Override
  53.  
  54.   public void onCreate(Bundle savedInstanceState) {
  55.  
  56.     super.onCreate(savedInstanceState);
  57.  
  58.     tv = new TextView(this);
  59.  
  60.     this.setContentView(tv);
  61.  
  62.  
  63.  
  64.     try {
  65.  
  66.    
  67.  
  68.       SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
  69.  
  70.       // request.addProperty("Celsius", "32");
  71.  
  72.      
  73.  
  74.       SoapSerializationEnvelope envelope =
  75.  
  76.         new SoapSerializationEnvelope(SoapEnvelope.VER11);
  77.  
  78.       envelope.dotNet = true;
  79.  
  80.       envelope.setOutputSoapObject(request);
  81.  
  82.      
  83.  
  84.       AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
  85.  
  86.       androidHttpTransport.call(SOAP_ACTION, envelope);
  87.  
  88.      
  89.  
  90.       SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
  91.  
  92.       String strResult = result.toString();
  93.  
  94.      
  95.  
  96.       tv.setText("Result: " + strResult);
  97.  
  98.       tv.invalidate();
  99.  
  100.     } catch (Exception e) {
  101.  
  102.       e.printStackTrace();
  103.  
  104.     }
  105.  
  106.   }
  107.  
  108. }
  109.  
  110.  
  111.  
  112.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Also, make sure that you add the line:

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

to your AndroidManifest.xml file of your projest.

I first tested the code using the tempuri Web Service. It worked fine. I then commented out this code and inserted the code to access my test Web Server.

If you don’t enable HttpSoap in the in your Web Service (step 2) you will get a null pointer exception in the line:

SoapObject result = (SoapObject) envelope.getResponse();

Please excuse the formatting of this article... it is my first. I didn't know how to format the Java code.

I hope this will help fellow Droids.

Charles
CharlesBrauer
Freshman
Freshman
 
Posts: 2
Joined: Mon Mar 01, 2010 10:02 pm

Top

Re: Accessing data from a Microsoft Web Service

Postby anuani20 » Tue Apr 27, 2010 11:32 am

Hey ,

I am a student and i am making an android application as my project. So I want to call a web service from android. I am new to android. Can you please send the source code or can i get more information about how the web services can be called android application.Kindly waiting for reply.

Regards



CharlesBrauer wrote:I have accessed my test Web Service with the Android 2.1 simulator. I am using the latest Eclipse and Java systems to build the Android application.

Here is what I did to make it work:

1. My Web Service that was built by Visual Studio 2008 and .Net 3.5.

The Web Service URL is:
http://www.cypresspoint.com/DroidServic ... rvice.asmx.
This address is live. Please try it in your browser.

2. To make my web site compatible with Android, I had to add:
<webServices>
<protocols>
<add name="HttpSoap"/>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
.
.
.
To the Visual Studio 2008 WebConfig.xml file.

3. I have downloaded ksoap2-android from:
http://code.google.com/p/ksoap2-android/.

4. I installed the jar-file using the instructions at:
http://developer.android.com/guide/appe ... nallibrary.

My Android test code is:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.hedgetools.testwebservice;
  2.  
  3. import org.ksoap2.SoapEnvelope;
  4. import org.ksoap2.serialization.SoapObject;
  5. import org.ksoap2.serialization.SoapPrimitive;
  6. import org.ksoap2.serialization.SoapSerializationEnvelope;
  7. import org.ksoap2.transport.AndroidHttpTransport;
  8.  
  9. import android.app.*;
  10. import android.os.*;
  11. import android.widget.TextView;
  12.  
  13. public class TestWebService extends Activity {
  14.   private static final String SOAP_ACTION = "http://www.CypressPoint.com/DroidService/HelloWorld";
  15.   private static final String METHOD_NAME = "HelloWorld";
  16.   private static final String NAMESPACE   = "http://www.CypressPoint.com/DroidService/";
  17.   private static final String URL = "http://www.CypressPoint.com/DroidService/DroidWebService.asmx";
  18.  
  19.   // private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
  20.   // private static final String METHOD_NAME = "CelsiusToFahrenheit";
  21.   // private static final String NAMESPACE   = "http://tempuri.org/";
  22.   // private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
  23.  
  24.   TextView tv;
  25.  
  26.   @Override
  27.   public void onCreate(Bundle savedInstanceState) {
  28.     super.onCreate(savedInstanceState);
  29.     tv = new TextView(this);
  30.     this.setContentView(tv);
  31.  
  32.     try {
  33.    
  34.       SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
  35.       // request.addProperty("Celsius", "32");
  36.      
  37.       SoapSerializationEnvelope envelope =
  38.         new SoapSerializationEnvelope(SoapEnvelope.VER11);
  39.       envelope.dotNet = true;
  40.       envelope.setOutputSoapObject(request);
  41.      
  42.       AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
  43.       androidHttpTransport.call(SOAP_ACTION, envelope);
  44.      
  45.       SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
  46.       String strResult = result.toString();
  47.      
  48.       tv.setText("Result: " + strResult);
  49.       tv.invalidate();
  50.     } catch (Exception e) {
  51.       e.printStackTrace();
  52.     }
  53.   }
  54. }
  55.  
  56.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Also, make sure that you add the line:

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

to your AndroidManifest.xml file of your projest.

I first tested the code using the tempuri Web Service. It worked fine. I then commented out this code and inserted the code to access my test Web Server.

If you don’t enable HttpSoap in the in your Web Service (step 2) you will get a null pointer exception in the line:

SoapObject result = (SoapObject) envelope.getResponse();

Please excuse the formatting of this article... it is my first. I didn't know how to format the Java code.

I hope this will help fellow Droids.

Charles
anuani20
Freshman
Freshman
 
Posts: 4
Joined: Tue Apr 27, 2010 11:24 am

I'm confused.

Postby CharlesBrauer » Tue Apr 27, 2010 6:16 pm

Hello,

You asked: "Can you please send the source code?". I'm confused. Your mail message shows ALL my source code. Please create a test project and cut and paste my code into you project. You should be able to access my test Web Service that is shown in my source code. Please give it a try. It should work for you.

Charles
CharlesBrauer
Freshman
Freshman
 
Posts: 2
Joined: Mon Mar 01, 2010 10:02 pm

Re: I'm confused.

Postby anuani20 » Tue Apr 27, 2010 8:47 pm

Thanku for ur reply...It is working for me.. Actually i want to use to the webservice for a login form...So can i get more information about the (android)web services .
Regards

CharlesBrauer wrote:Hello,

You asked: "Can you please send the source code?". I'm confused. Your mail message shows ALL my source code. Please create a test project and cut and paste my code into you project. You should be able to access my test Web Service that is shown in my source code. Please give it a try. It should work for you.

Charles
anuani20
Freshman
Freshman
 
Posts: 4
Joined: Tue Apr 27, 2010 11:24 am

Re: Accessing data from a Microsoft Web Service

Postby anuani20 » Wed Apr 28, 2010 8:46 pm

Can any one help me how to create an object for a web service in android and How to pass networkcredentials in android for .Net web service?


Kindly waiting for a reply....
anuani20
Freshman
Freshman
 
Posts: 4
Joined: Tue Apr 27, 2010 11:24 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests