Call SOAP web service in Android
Web Services:
By using web services, we can transfer data
between the electronic devices. If we create web services for one platform and
it can be accessible from any of the platform. There are many types of web
services. Some of them are,
·
JSON
·
SOAP
Soap Web Service:
Simple Object Access Protocol (SOAP) is a
standard protocol specification for message exchange based on XML.
Communication between the web service and client happens using XML messages.
Steps To Invoke Soap Web Service
1.Download ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar
and paste into libs folder.
2.
You can download ksoap2-android-assembly-2.5.2-jar-with dependencies.jar here.
3. Create Android project with name as "MobileAppSecurityCode".
Set Main Activity name as "GenerateCodeActivity". This is the code
for GenerateCodeActivity.
package com.etr.securitycode;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class GenerateCodeActivity extends Activity {
private String METHOD_NAME = "CodeGenerate";
private String NAMESPACE = "http://tempuri.org/";
private String
SOAP_ACTION = "http://tempuri.org/IService1/CodeGenerate";
private static final String URL =
"http://easprojects.sify.net/MyServices/Service1.svc?wsdl";
Object result;
EditText username, password, code;
Button generate;
NetWorkStatus status;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_generate_code);
username = (EditText) findViewById(R.id.editText1);
password = (EditText) findViewById(R.id.editText2);
code = (EditText) findViewById(R.id.editText3);
generate = (Button) findViewById(R.id.button1);
status = new NetWorkStatus(this);
generate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (status.isConnecting()) {
showAlertDialog(GenerateCodeActivity.this,
"Internet connection",
"You have internet connection");
new Invoke().execute();
} else {
showAlertDialog(GenerateCodeActivity.this,
"No Internet connection",
"You don't have internet connection");
}
}
});
} catch (Exception e) {
Log.v("Exception", Log.getStackTraceString(e));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.generate_code, menu);
return true;
}
public class Invoke extends AsyncTask<Void, Void, Void> {
ProgressDialog dialog;
protected void onPreExecute() {
dialog = new ProgressDialog(GenerateCodeActivity.this);
dialog.setMessage("Please wait...");
dialog.setCancelable(false);
dialog.show();
}
protected Void doInBackground(Void... arg0) {
try {
SoapObject
request = new
SoapObject(NAMESPACE, METHOD_NAME);
request.
addProperty("userName", username.getText().toString());
request.
addProperty("password", password.getText().toString());
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport =
new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
result = envelope.getResponse();
} catch (Exception e1) {
Log.v("JSONCall Exception", Log.getStackTraceString(e1));
}
return null;
}
protected void onPostExecute(Void name) {
username.setText("");
password.setText("");
code.setText(result.toString());
dialog.dismiss();
}
}
public void showAlertDialog(Context context, String title, String msg) {
AlertDialog.Builder builder = new AlertDialog.Builder(
GenerateCodeActivity.this);
builder.setTitle(title).setMessage(msg).setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
4. You can get URL and soapAction from below screenshot
5. Create "Network Status" class. It will check the status of your
network connection.
package com.etr.securitycode;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NetWorkStatus {
private Context _context;
public NetWorkStatus(Context context) {
this._context = context;
}
public boolean isConnecting() {
ConnectivityManager connectivity = (ConnectivityManager) _context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
}
6. Add following permission in manifest file
<uses-permission
android:name="android.permission.INTERNET"></uses-permission>
|
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE">
</uses-permission>
|
7. This is code for activity_generate_code.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".GenerateCodeActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="58dp"
android:background="@drawable/shapes"
android:ems="10"
android:hint="@string/u_name"
android:inputType="textPersonName"
android:minHeight="50dp"
android:paddingLeft="10dp"
android:textColor="@android:color/white" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText3"
android:layout_centerHorizontal="true"
android:layout_marginTop="51dp"
android:background="#0072c6"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/g_code"
android:textColor="@android:color/white" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:background="@drawable/shapes"
android:ems="10"
android:hint="@string/password"
android:inputType="textPassword"
android:minHeight="50dp"
android:paddingLeft="10dp"
android:textColor="@android:color/white" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_alignRight="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="46dp"
android:background="@drawable/shapes"
android:editable="false"
android:ems="10"
android:minHeight="50dp"
android:paddingLeft="10dp"
android:textColor="@android:color/white" >
<requestFocus />
</EditText>
</RelativeLayout>
Comments
Post a Comment