How to call a local web service from an Android mobile application

Somehow I managed to solve my problem and I am answering this question so that it be can be useful to people who see this post in the future.

So here is the modified code:

             package com.demo;
             import java.net.SocketException;
             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.app.Activity;
             import android.app.ProgressDialog;
             import android.os.AsyncTask;
             import android.os.Bundle;
             import android.util.Log;
             import android.view.View;
             import android.view.View.OnClickListener;
             import android.widget.Button;
             import android.widget.EditText;

     public class Login extends Activity {

  private final String NAMESPACE = "http://tempuri.org/";
  private final String URL = "http://10.0.2.2/testlogin/Service1.asmx";

    String user_id;
    String password;

  @Override
   public void onCreate(Bundle savedInstanceState)
      {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button signin = (Button) findViewById(R.id.regsubmitbtn);
        signin.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {


                        EditText etxt_user = (EditText) findViewById(R.id.usereditlog);
                        user_id = etxt_user.getText().toString();
                        EditText etxt_password = (EditText)  findViewById(R.id.pwdeditlog);
                        password = etxt_password.getText().toString();

                        new LoginTask().execute();

                }
           });   
     }

    private boolean doLogin(String user_id, String password) {   

   boolean result=false;
   final String SOAP_ACTION = "http://tempuri.org/GetLogin";
   final String METHOD_NAME = "GetLogin";     
   SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

   request.addProperty("userid", user_id);
   request.addProperty("password",password);
   SoapSerializationEnvelope envelope = new   SoapSerializationEnvelope(SoapEnvelope.VER11);
   envelope.dotNet = true; // Set this variable to true for
                                                           // compatibility with what seems to be the
                                                           // default encoding for .Net-Services.
   envelope.setOutputSoapObject(request);

   System.out.println(request);

   HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);


   try {
           androidHttpTransport.call(SOAP_ACTION, envelope);
           SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
           Log.i("myApp", response.toString());
           System.out.println("response" +response);

                   if(response.toString().equalsIgnoreCase("success"))
       {
                   result = true;

       }

   }catch(SocketException ex)
   {
       Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage());
       ex.printStackTrace();
   }
   catch (Exception e) {
       Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
           e.printStackTrace();
   }
   return result;

   }


private class LoginTask extends AsyncTask<Void, Void, Void> {

    private final ProgressDialog dialog = new ProgressDialog(
                    Login.this);

    protected void onPreExecute() {

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

    }


    protected Void doInBackground(final Void... unused) {

        boolean auth=doLogin(user_id,password);
        System.out.println(auth);

        return null;// don't interact with the ui!
    }


    protected void onPostExecute(Void result) {


            if (this.dialog.isShowing()) {
            this.dialog.dismiss();
        }         
      }

    } 

   } 

Leave a Comment