How to communicate WCF webservices in android applications

- First you have to create wcf webserices, then you have defines some necessary thing to communicate with wcf.

You have to recognize the following in your wsdl file:

Method Name and Soap Action:


Namespace:

URL:

Download ksoap 2 and import in libs folder in android:
           

To download a file from there, right click on "View raw file" and select "Save Link as" (this label differs for different browsers) and you will get the full jar downloaded.

The latest release artifact would be available at

with a direct download url of

Now you have to defines following java code in your android application:

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.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

public class Test extends Activity {

public class SoapTask extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(Test.this);
protected void onPreExecute() {

this.dialog.setMessage("Checking in...");
this.dialog.show();

}

@Override
protected Void doInBackground(Void... arg0) {
try{
WebServiceCallExample();
}catch (Exception e){
e.printStackTrace();
}
return null;
}

protected void onPostExecute(Void result) {
TextView tv = (TextView) findViewById(R.id.Result);
tv.setText(test_string);
if (this.dialog.isShowing()){
this.dialog.dismiss();
}
}

}

private String test_string;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
new SoapTask().execute();
}

@SuppressWarnings("deprecation")
public void WebServiceCallExample() {
final String NAMESPACE = "http://tempuri.org/";
final String URL = "http://yoururl.com/testService.svc";
final String METHOD_NAME = "SayHello";
final String SOAP_ACTION = "http://tempuri.org/IWFPService/SayHello";

SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

/* Set the web service envelope */
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(Request);
System.out.println("Request::" + Request.toString());

AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
try{
androidHttpTransport.call(SOAP_ACTION, envelope);
//SoapObject response = (SoapObject) envelope.getResponse();
SoapPrimitive sp = (SoapPrimitive) envelope.getResponse();

SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
System.out.println("Response::" + resultsRequestSOAP.toString());
test_string = sp.toString();
}catch (Exception e){
e.printStackTrace();
}
}
}


test.xml layout file here:

<?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:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/Result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="7dip"
        android:layout_marginRight="7dip"
        android:gravity="fill_vertical|fill_horizontal"
        android:layout_gravity="center_horizontal"
         android:textSize="24dip" android:text="test"
        android:textStyle="bold" >
    </TextView>

</LinearLayout>


You have got following output:



If you have any Problem please go to these link, which is more helpful for you:
http://stackoverflow.com/questions/tagged/android+ksoap2
https://code.google.com/p/ksoap2-android/wiki/Links
http://www.wsdl2code.com/pages/Example.aspx
http://code.tutsplus.com/tutorials/consuming-web-services-with-ksoap--mobile-21242
http://easywsdl.com/
How to communicate WCF webservices in android applications How to communicate WCF webservices in android applications Reviewed by D Kshetri on 10:26 PM Rating: 5

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hey, D Kshetri,

    I've used your code as a template to help me with this issue, but I am getting nowhere. I am getting an XmlPullParserException. I have posted my problem on Stack Overflow. Could you take a look at it?

    http://stackoverflow.com/questions/35778131/xmlpullparserexception-unexpected-token-positiontext-issue-with-java-android

    ReplyDelete

Powered by Blogger.