SOAP IMPLEMENTATION WITH GENERIC CALLBACK METHOD IN ANDROID
Posted By : Arpit Sharma | 08-Mar-2014
- NetworkCall.java is use for SOAP implementation and calling.
- NetworkTask.java is use for Async call handling by AsyncTasks with progress dialog(optional).
- NetworkTaskComnpleteListener.java is a genric interface with callback method.
Now i will show and explain the working and implementaion of each file.
# NetworkCall.java
- First of all you have to download KSOAP2.jar file and put it into the library foldar as shown below.
- Copy and paste the following code into NetworkCall.java file.
package com.oodles.network;
import java.util.Enumeration;
import java.util.Hashtable;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.content.Context;
public class NetworkCall {
String SOAPNameSpace = "Your SOAP name space";
String SOAPUrl = "Your SOAP url";
int networkTimeOut = 30 * 1000;// 30 seconds
Context mContext;
public NetworkCall(Context context) {
mContext = context;
}
// Get SOAP Action Name
public String getSoapAction(String method) {
return "\"" + SOAPNameSpace + method + "\"";
}
public String postDataToSOAPService(Hashtable<string, string> hashTable,
String methodName) throws Exception {
//Create a SOAP Object.
SoapObject request = new SoapObject(SOAPNameSpace, methodName);
//HashTable Enumeration for passing number of params along with there keys
Enumeration<string> enumer = hashTable.keys();
while (enumer.hasMoreElements()) {
String key = (String) enumer.nextElement();
request.addProperty(key, hashTable.get(key));
}
//Create a serializable Object.
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
//SOAP is implemented in dotNet true/false.
envelope.dotNet = true;
//Set request data into envelope and send request using HttpTransport
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(SOAPUrl,
networkTimeOut);
androidHttpTransport.call(getSoapAction(methodName), envelope);
//Get Response Data in form of SOAPObject
SoapObject result = (SoapObject) envelope.bodyIn;
//Free all the object after use for better performance
request = null;
envelope = null;
androidHttpTransport = null;
//Return Result Response Data
return result.toString();
}
}
# NetworkTask.java
- This file is responsibe for network thread hendling using AsyncTask.
- Copy and pase the given code into NetworkTask.java file.
package com.oodles.network;
import java.util.Hashtable;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
public class NetworkTask extends AsyncTask<string, string> {
Context mContext;
NetworkTaskCompleteListener mListener;
Hashtable<string, string> mHashTable;
Boolean mProgress = true;
public NetworkTask(Context context, Hashtable<string, string> hashTable,
boolean progress) {
mContext = context;
mListener = (NetworkTaskCompleteListener) context;
mHashTable = hashTable;
mProgress = progress;
}
ProgressDialog pd;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
if (mProgress) {
pd = ProgressDialog.show(mContext, null, "Loading");
}
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String result = null;
try {
result = new NetworkCall(mContext).postDataToSOAPService(
mHashTable, params[0]);
} catch (Exception ee) {
}
return result;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (mProgress) {
pd.dismiss();
}
mListener.onNetworkComplete(result);
}
}
# NetworkTaskCompleteListener.java
- NetworkTaskCompleteListener is a interface with onNetworkComplete callback method.
- This file is responsible for network callback functionality.
package com.oodles.network;
public interface NetworkTaskCompleteListener {
public void onNetworkComplete(String result);
}
Congratulations, you have done with the basic structure of SOAP and Callback method , now it time to implement it.
- Implement NetworkTaskCompleteListener in your activity and override onNetworkComplete for callback.
- Call NetworkTask, pass params and SOAP method name as show in given code.
package com.oodles.soapdemo;
import java.util.Hashtable;
import com.oodles.network.NetworkTask;
import com.oodles.network.NetworkTaskCompleteListener;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements NetworkTaskCompleteListener{
Context mContext = MainActivity.this;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//------------------------------------------------------------------------------------------------
//Create a Hash Table for passing the params and keys
Hashtable<string, string> ht = new Hashtable<string, string>();
//Keys String Array
String[] keys = { "username", "password" };
String[] values = {"demo", "demo"};
//Put key and value into hashTable
for (int i = 0; i < keys.length; i++) {
ht.put(keys[i], values[i]);
}
//Network calling
new NetworkTask(mContext, ht, true)
.execute("Soap method name");
ht = null;
//--------------------------------------------------------------------------------------------------
}
});
}
//This method is responsible for network callback
@Override
public void onNetworkComplete(String result) {
//Show network response result
Toast.makeText(mContext, result, Toast.LENGTH_LONG).show();
}
}
DOWNLOAD SOURCE CODE
Happy Coding :)
ARPIT SHARMA
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Arpit Sharma
Arpit has been developing Android and iPhone applications for a while now and is expert in mobile application development . He excels in developing mobile applications with location based intelligence. Arpit loves Modelling as a hobby.