Search code examples
android.netsql-serverweb-servicesksoap2

SOAP web service returning incorrect response


I am working on localhost ..I have made a web service in .NET that takes in user name and password and authenticates the user..My web service code is:http://pastebin.com/GQGtKSqq and my log cat shows this:http://pastebin.com/czs5Fbc3

My log cat shows that the output is a statement from the catch statement of my web service..It should actually display the statement in try block which is "success".. When I am calling the web service from my Android app I am not getting the same response as I am getting when I run the web service in my browser. I have stored user name and password in a SQL database..Can anyone help solve my problem . My android code is as follows:

       package com.demo;

       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.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 static final String SOAP_ACTION = "http://tempuri.org/GetLoginDetails";
private static final String METHOD_NAME = "GetLoginDetails";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://10.0.2.2/testlogin/Service1.asmx";

@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) {

                    String user_id;
                    String password;
                        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 class LoginTask extends AsyncTask<Void, Void, Void> {

        String auth = null;
         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) {

            auth = doLogin("hello", "hello");
            return null; // don't interact with the ui!
    }

    protected void onPostExecute(Void result) {
            if (this.dialog.isShowing()) {
                    this.dialog.dismiss();
            }
        System.out.println(auth);
    }
     }


      private String doLogin(String user_id, String password) {

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("user", user_id);
           request.addProperty("password", password);
              SoapSerializationEnvelope soapenvelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
           soapenvelope.dotNet = true; // Set this variable to true for
          soapenvelope.setOutputSoapObject(request);

    AndroidHttpTransport httptransport = new AndroidHttpTransport(URL);
    try {
            httptransport.call(SOAP_ACTION, soapenvelope);
            SoapPrimitive resultstring = (SoapPrimitive) soapenvelope
                            .getResponse();
            Log.d("Authenticaion", resultstring+"");
            System.out.println(resultstring);

            return resultstring + "";

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

Solution

  • ry this :I did like this

    // ksoap2 calling wcf
    public SoapPrimitive soapPrimitiveData(String tablename,String strBusinessUnit, String strExecutive, String strTerritoryCode,String strUField1) throws IOException,XmlPullParserException {
        SoapPrimitive responsesData = null;
        SoapObject requestData = new SoapObject(NAMESPACE, METHOD_TABLEDATA); // set
    
        Date date = new Date();
        DateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
        String strDate = formatter.format(date);
    
        requestData.addProperty("strBusinessUnit", strBusinessUnit);
        requestData.addProperty("strExecutive", strExecutive);
        requestData.addProperty("strTableName", tablename);
        requestData.addProperty("strDate", strDate);
        requestData.addProperty("strTerritoryCode", strTerritoryCode);
        requestData.addProperty("strUField1", strUField1);
        SoapSerializationEnvelope envelopes = new SoapSerializationEnvelope(SoapEnvelope.VER11); // put all required data into a soap//// envelope
        envelopes.dotNet = true;
        envelopes.setOutputSoapObject(requestData);
        AndroidHttpTransport httpTransport = new AndroidHttpTransport(APPURL);
        httpTransport.debug = true;
    
        try {
            httpTransport.call(SOAP_ACTION1, envelopes);
            responsesData = (SoapPrimitive) envelopes.getResponse();
    
    
        } 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 responsesData;
    }
    

    You code is correct.

    In your 'onpostexecute() you have to write next activity calling part...

             @Override
        protected void onPostExecute(Void result) {
    
            if (this.dialog.isShowing()) {
                this.dialog.dismiss();
            }
            Toast.makeText(DownlaodTableActivity.this,"All The tables Downloaded Successfully",Toast.LENGTH_SHORT).show();
    
    
            if(comingFrom.equals("Success")){
                Intent i = new Intent(getBaseContext(), SettingScreenActivity.class);
                View vi = SettingActivityGroup.group.getLocalActivityManager().startActivity("SettingScreenActivity", i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
                SettingActivityGroup.group.replaceView(vi);
    
    
            }else{
                Intent showContent = new Intent(getApplicationContext(),MainActivity.class);
                startActivity(showContent);
            }
        }