Pages

Friday 26 July 2013

Consume .net webservice in android



This simple example demonstrates how we can access a .net web service  from Android application.

 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" >

    <Button
        android:id="@+id/web_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="112dp"
        android:layout_marginTop="146dp"
        android:text="Web Service" />

    <TextView
        android:id="@+id/fetch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="TextView" />

    <ImageView
        android:id="@+id/fetch_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/fetch"
        android:layout_below="@+id/fetch"
        android:layout_marginTop="52dp"
        android:src="@drawable/ic_action_search" />

</RelativeLayout>

Activity:

public class MainActivity extends Activity {

Button web_service;
TextView fetch_service;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

fetch_service = (TextView) findViewById(R.id.fetch);
web_service = (Button) findViewById(R.id.web_service);
web_service.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myasynctask MAT = new myasynctask();
MAT.execute();
}
});
}

class myasynctask extends AsyncTask<String, Void, String> {

String str;
private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private static final String METHOD_NAME = "CelsiusToFahrenheit";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";

@Override
protected String doInBackground(String... params) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Celsius", "32");

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);

HttpTransportSE ht = new HttpTransportSE(URL);

try {
ht.call(SOAP_ACTION, envelope);
final SoapPrimitive response = (SoapPrimitive) envelope
.getResponse();
str = response.toString();

} catch (Exception e) {
e.printStackTrace();

}
Log.d("WebRespone", str);
return str;
}

@Override
public void onPostExecute(String result) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
fetch_service.setText(result);
super.onPostExecute(result);
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}

}
}
Manifest:
add the following thins in your manifest
 <uses-permission android:name="android.permission.INTERNET"/>

To Do:

You Must add this KSOAP jar file in your Lib folder




No comments:

Post a Comment