Android – Accessing Webservices using SOAP Example
Source main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Celsius to Farenheit" />
<EditText
android:id="@+id/editText1"
android:layout_width=" fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:singleLine="true" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Convert to Farenheit" />
<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
Source WebserviceActivity.java
package com.webserviceexample;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class WebserviceActivity extends Activity {
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
private final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private final String METHOD_NAME = "CelsiusToFahrenheit";
Button b;
TextView tv;
EditText et;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText) findViewById(R.id.editText1);
tv = (TextView) findViewById(R.id.tv_result);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String result = getFahrenheit(et.getText().toString());
tv.setText(result+"° F");
}
});
}
public String getFahrenheit(String celsius) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo celsiusPI = new PropertyInfo();
celsiusPI.setName("Celsius");
celsiusPI.setValue(celsius);
celsiusPI.setType(double.class);
request.addProperty(celsiusPI);
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("Webservice Output", response.toString());
return response.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Note:
· Add Internet Permission in your AndroidManifest.xml file.
· Download ksoap2.jar and add to buildpath.
The output will be
i am using this code. but i am getting output as null0 F
ReplyDeletethanks...! it works fine
ReplyDeletei am using your code. but i am getting output as null0 F
ReplyDeletenice blog ! i think all having prob because of webservice
ReplyDeleteit gives my this error
ReplyDeletethe application has stopped unexpected.please try again .
Thanks.This code works fine.nice work
ReplyDeletehi, can you suggest me which is the best json or soap ? main thing is security and code efficiency . because up to now i am just using only json. so if soap is better then json then i will use that. but i think its too lengthy for code developing compare to json.
ReplyDeletenice code dude
ReplyDeletereturn null; is not required.
ReplyDeleteThis comment has been removed by the author.
ReplyDeletethe application has stopped unexpected.please try again
ReplyDeleteNot working i get only null value
ReplyDeleteweb hosting Pakistan
ReplyDeleteAs an entrepreneur, you only think about your business venture and leave everything to us.
this not working
ReplyDeleteIts a great example given by you that is provide the logic of how android access the web services using the SOAP.Through this blog i have got the ideas to implement the android apps...Thanks
ReplyDeleteAndroid App Development
I feel pleasure to read the content that you are posting.
ReplyDeleteclick over here now
Thanks for sharing apk Games this great list. There is no doubt it’s important to do a natural link building without spamming to gain better places in Google.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis code is very help for me..........
ReplyDeleteThanks.....
Excellent article you have shared thanks for sharing. I appreciated of you.iPhone Apps Development Company
ReplyDeleteThanks for sharing such a nice post Android App Development
ReplyDeleteGood work by the blog writer! such a nice and informative article keep sharing more articles.
ReplyDeleteappsocio
i have got the many ideas form your post. keep posting.
ReplyDeleteThank you for sharing such informative post.
ReplyDeleteOn-demand Startups | pitch deck examples
Hello there! I just want to offer you a big thumbs up for your great info you have right here on this post. I'll be coming back to your web site for more soon.
ReplyDeleteDigital Marketing Company in Pune | Software Development Company
hgow i can upload pdf file using ksoap ? my email id vaishali27.idnani@gmail.com
ReplyDeleteMacular Degeneration
ReplyDeleteThis is a great post. So clear and easy to follow. Thanks for the tangible and attainable help. All your hard work is much appreciated.
Macular Degeneration Glasses Electronic Glasses
ReplyDeleteHighly Commendable blog! Keep up the great work.Software Development Company in India
ReplyDeleteThis is a nice post in an interesting line of content.Thanks for sharing this article, great way of bring this topic to discussion.
ReplyDeleteandroid app development in Mumbai
iphone app development in Mumbai
ios apps development Bangalore
android app development in bangalore
SOAP was developed as an intermediate language so that applications built on various programming languages could talk easily to each other and avoid the extreme development effort. Most app development company are emphasis on this technology.
ReplyDeleteapp development company
Hi Team,
ReplyDeleteI just stumbled on your post.
Good Stuff!
I'm just reaching out because I recently published an article on 9 Benefits of cloud computing for businesses that might be a good fit.
Visit: https://www.passogen.com
Either way, keep up the awesome work. :-)
Good valuable information... Thanks for the sharing this.
ReplyDeleteAndroid game development company
Great post Thank you for sharing such a informative article. So clear and easy to follow. Thanks for the tangible and attainable help. All your hard work is much appreciated.
ReplyDeleteLow Vision aids
Awesome post. Uniqwebtech, Top digital marketing company offering Digital services namely SEO,SMO,SEM,Content Marketing, Video Marketing, Web development and Social Media Marketing Solutions.
ReplyDeleteGreat post Thank you for sharing such a informative article. So clear and easy to follow.
ReplyDeletemobile app development company in san francisco
Really nice article thank you for sharing such a informative article. So clear and easy to follow. Thanks for the tangible and attainable help.
ReplyDeleteAugmented Reality App development
Thanks for sharing such a greatinformation I must say very informative blog post. Keep it up!!
ReplyDeletefree ebook for mobile app development
Highly Commendable blog! Keep up the great work.i have got the many ideas form your post. keep posting.
ReplyDeleteFree guide for macular degeneration glasses
Main thing is security and code efficiency . because up to now i am just using only json. so if soap is better then json then i will use that.
ReplyDeleteEye Health
I would like to thank you for the efforts you had made for writing this wonderful piece of writing.
ReplyDeleteCost to build app like Practo
Redbus like App development cost
taxi booking app like ola
Hi Jack!!!
ReplyDeleteNice post. its a great example given by you that is provide the logic of how android access the web services using the SOAP.Through this blog i have got the ideas to implement the android apps.
Android App Development Company in Pune
Thank you for sharing this valuable content with us.
ReplyDeleteSr. Software Develop
Sagacity Solutions India
great post. Thanks for sharing.
ReplyDeleteWe are an experienced team in one of the Best software company and product specialist for software development and implementation. Sovereign provides Website Design, Wordpress Development and Mobile App Development, Digital marketing and SEO Services.
Really great thoughtful InformaHire android app developerstion thank you!
ReplyDeleteGreat to see such blog posts. Nice topic shared here. I learn a lot from you and the way of explanation is just wow. Thanks for such a beautiful explanation.
ReplyDeletesmart audit
Great to see such blog posts thanks for posting
ReplyDelete