In this post we will go through a few steps to be followed to handle SOAP web services for Android, which are created in .Net
Above code is for sample and working with little modifications.
Feel free to post comments/doubts back.
Below are the steps I have followed to handle web services in Android using KSOAP. However WSClient++ is cool too.
1. Download the KSOAP library (.jar file) from below link.
KSOAP2 [In the page opened by this link, Click on "view raw file" to get jar file]
2. Add the downloaded jar file to your application build path by following below instructions:
Right click on your application name in the package explorer> Click on “Build Path”>Click on “Configure Build Path”>Click on “Java Build Path” from the left most panel from new window opened> Click on “Libraries”> Click on “Add External jars” from list of buttons located in right panel> Browse for the downloaded jar file(ksoap2-android-assembly-2.3-jar-with-dependencies.jar) just downloaded.>Click on “OK”.
3. My calss which handles SOAP request and response looks like below:
package org.sample.code;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class Main extends Activity {
/** Called when the activity is first created. */
//You can get SOAP_ACTION from the
//sample SOAP xml structure for the corresponding operation.
private static final String SOAP_ACTION ="http://tempuri.org/CelsiusToFahrenheit";
private static final String METHOD_NAME ="CelsiusToFahrenheit";
private static final String NAMESPACE ="http://tempuri.org/";
//Make sure URL ends with ".asmx"
private static final String URL ="http://www.w3schools.com/webservices/tempconvert.asmx";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.textview);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//Add the parameters to be paused..
request.addProperty("Celsius", "32");
//In case of multiple parameters,
// request.addProperty("param2", "val2");
// val2 with in double quote,if val2 has to be passed as String
// request.addProperty("param3", "val3");
// val3 with in double quote,if val3 has to be passed as String
//etc..
SoapSerializationEnvelope mySoapEnvelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//Set this true, iff its created using .Net webservice.
mySoapEnvelop.dotNet=true;
mySoapEnvelop.setOutputSoapObject(request);
AndroidHttpTransport myAndroidHttpTransport = new AndroidHttpTransport(URL);
try
{
myAndroidHttpTransport.call(SOAP_ACTION, mySoapEnvelop);
SoapPrimitive resultString = (SoapPrimitive) mySoapEnvelop.getResponse();
//You may parse "resultStrng" as you wish..
//You may parse "resultStrng" as you wish..
tv.setText("result = "+ resultString);
}
catch( Exception e )
{
Log.d("ERROR" , "exception e = "+ e.getMessage());
tv.setText("result = "+ e.toString());
}
}
}
4. Add the below permission to your manifest file, just before closing tag of “manifest” :
<uses-permission android:name="android.permission.INTERNET" />
Above code is for sample and working with little modifications.
Feel free to post comments/doubts back.
Happy coding :-)
Hi, can you please send me the complete code as eclipse project on my gmail id ?
ReplyDeleteMy gmail id is javahunterand@gmail.com