Hello Friends!
I am going to share how to consume soap web services in Android. Important steps are given below-
1)Create new android project name e.g.- KsoapTest
2)Download ksoap2-j2se-full-2.1.2 from ksoap offical website you can use Google for searching.
3)Right click on project and then click on “Build path” then “Configure Build Path”.
4)From Libraries Tab choose “Add Extarnal JARs” then choose your ksoap2-j2se-full-2.1.2 jar file from downloads and click OK.
5)Now Copy – Paste all code in your project and enjoye codeing...
1-Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ksoap.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Ksoap2TestActivity"
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>
</manifest>
2-KsoapTestActivity.java
package ksoap.test;
import java.util.ArrayList;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.widget.TextView;
public class KsoapTestActivity extends Activity {
private final String NAMESPACE = "http://DefaultNamespace/";
private final String URL = "http://10.0.0.17:8080/SecurityAppWebService/services/AppServices?wsdl";
private final String SOAP_ACTION = "http://DefaultNamespace/AppServices";
private final String METHOD_NAME = "getDetails";
ArrayList<Guard> guardList=new ArrayList<Guard>();
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo propInfo=new PropertyInfo();
propInfo.type=PropertyInfo.INTEGER_CLASS;
//adding parameters
request.addProperty("a", 10);
request.addProperty("b", 15);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject)envelope.getResponse();
Log.e("Object response", response.toString());
TextView tv = new TextView(this);
tv.setText("Output: "+response.toString());
setContentView(tv);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3-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" />
</LinearLayout>
No comments:
Post a Comment