Sunday, February 9, 2014

ANDROID APPLICATIONS  

How to Call a Dotnet Web Service using Android App.

package com.cineplexbd.movieticket;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MnWebService extends Activity {
     TextView result;
     final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
     final String METHOD_NAME = "HelloWorld";
     final String NAMESPACE = "http://tempuri.org/";
     final String URL = "http://192.168.10.13/WebService/TestWebService.asmx";
//Or //use any 1 String URL
//final String URL = "http://mmm.com/SampleService.svc?wsdl";

     Object response;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mn_web_service);
         result = new TextView(this);

         setContentView(result);
         new backMethod().execute();
    }
    public class backMethod extends AsyncTask<String, Object, Object > {

       
         private final ProgressDialog dialog = new ProgressDialog(MnWebService.this);   
                                    
                                        @Override
                                        protected void onPreExecute() {
                                    
                                                        this.dialog.setMessage("Checking...");             
                                                         this.dialog.show(); 
                                        }

                                        @Override
                                        protected void onCancelled(Object result) {
                                                    
                                                        super.onCancelled(result);
                                        }

                                        @Override
                                        protected void onPostExecute(Object result) {
                                       //Here All your UI part is Done
                                    
                                                        if (result != null) {
                                                                        String myOutput=result.toString();
                                                                        MnWebService.this.result.setText(myOutput);
                                                                        setContentView(MnWebService.this.result);
                                                    
                                                        } else {
                                                                        Toast.makeText(getApplicationContext(),
                                                               "Result Found is ==  " + result + "", Toast.LENGTH_LONG)
                                                                                                        .show();
                                                        }
                                                        super.onPostExecute(result);
                                                        if (this.dialog.isShowing()) {

                                                                        this.dialog.dismiss();
                                                        }
                                                        super.onPostExecute(result);
                                        }

                                    
                                        @Override
                                        protected Object doInBackground(String... params) {
                                                                                    
                                        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

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

                                                                        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                                                                        androidHttpTransport.call(SOAP_ACTION, envelope);
                                                                    
                                                                        response = (SoapPrimitive) envelope.getResponse();
                                                                                     //here SoapPrimitive is an important part                                                                                             
                                                        } catch (Exception e) {
                                                                        e.printStackTrace();
                                                        }
                                                        return response;
                                        }
                        }
   
}


To  Download Ksoap2, Click here

 

No comments:

Post a Comment