Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am trying to log in to the .asmx website which is online from my android application i created three classes as follows

CheckDNLoginActivity.java
HomeActivity.java
WebService.java as follows:

Here is my CheckDNLoginActivity.java


Java
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

public class CheckDNLoginActivity extends Activity {
//Set Error Status
static boolean errored = false;
Button b;
TextView statusTV;
EditText userNameET , passWordET;   
ProgressBar webservicePG;
String editTextUsername;
boolean loginStatus;
String editTextPassword;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Name Text control
    userNameET = (EditText) findViewById(R.id.editText1);
    passWordET = (EditText) findViewById(R.id.editText2);
    //Display Text control
    statusTV = (TextView) findViewById(R.id.tv_result);
    //Button to trigger web service invocation
    b = (Button) findViewById(R.id.button1);
    //Display progress bar until web service invocation completes
    webservicePG = (ProgressBar) findViewById(R.id.progressBar1);
    //Button Click Listener
    b.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            //Check if text controls are not empty
            if (userNameET.getText().length() != 0 && userNameET.getText().toString() != "") {
                if(passWordET.getText().length() != 0 && passWordET.getText().toString() != ""){
                    editTextUsername = userNameET.getText().toString();
                    editTextPassword = passWordET.getText().toString();
                    statusTV.setText("");
                    //Create instance for AsyncCallWS
                    AsyncCallWS task = new AsyncCallWS();
                    //Call execute
                    task.execute();
                }
                //If Password text control is empty
                else{
                    statusTV.setText("Please enter Password");
                }
            //If Username text control is empty
            } else {
                statusTV.setText("Please enter Username");
            }
        }
    });
}
    private class AsyncCallWS extends AsyncTask<string,void,void> {
    @Override
    protected Void doInBackground(String... params) {
        //Call Web Method
        loginStatus =         WebService.invokeLoginWS(editTextUsername,editTextPassword,"AuthenticateUser");
        return null;
    }

    @Override
    //Once WebService returns response
    protected void onPostExecute(Void result) {
        //Make Progress Bar invisible
        webservicePG.setVisibility(View.INVISIBLE);
        Intent intObj = new Intent(CheckDNLoginActivity.this,HomeActivity.class);
        //Error status is false
        if(!errored){
            //Based on Boolean value returned from WebService
            if(loginStatus){
                //Navigate to Home Screen
                startActivity(intObj);
            }else{
                //Set Error message
                statusTV.setText("Login Failed, try again");
            }
        //Error status is true   
        }else{
            statusTV.setText("Error occured in invoking webservice");
        }
        //Re-initialize Error Status to False
        errored = false;
    }

    @Override
    //Make Progress Bar visible
    protected void onPreExecute() {
        webservicePG.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}   






Here is my HomeActivity.java code:

Java
import android.app.Activity;
import android.os.Bundle;

public class HomeActivity extends Activity {

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




WebService.java


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


public class WebService {
//Namespace of the Webservice - can be found in WSDL
private static String NAMESPACE = "http://tempuri.org/";
//Webservice URL - WSDL File location   
private static String URL = "http://namastii.co.in/Service.asmx";
//SOAP Action URI again Namespace + Web method name
private static String SOAP_ACTION = "http://tempuri.org/GetUserDetails";

public static boolean invokeLoginWS(String username,String passWD, String webMethName) {
    boolean loginStatus = false;
    // Create request
    SoapObject request = new SoapObject(NAMESPACE, webMethName);
    // Property which holds input parameters
    PropertyInfo userName = new PropertyInfo();
    PropertyInfo password = new PropertyInfo();
    // Set Username
    userName.setName("username");
    // Set Value
    userName.setValue(username);
    // Set dataType
    userName.setType(String.class);
    // Add the property to request object
    request.addProperty(userName);
    //Set Password
    password.setName("password");
    //Set dataType
    password.setValue(passWD);
    //Set dataType
    password.setType(String.class);
    //Add the property to request object
    request.addProperty(password);
    // Create envelope
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    // Set output SOAP object
    envelope.setOutputSoapObject(request);
    // Create HTTP call object
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        // Invoke web service
        androidHttpTransport.call(SOAP_ACTION+webMethName, envelope);
        // Get the response
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        // Assign it to  boolean variable variable
        loginStatus = Boolean.parseBoolean(response.toString());

    } catch (Exception e) {
        //Assign Error Status true in static variable 'errored'
        CheckDNLoginActivity.errored = true;
        e.printStackTrace();
    }
    //Return booleam to calling object
    return loginStatus;
}
}
Posted
Updated 8-Aug-14 4:50am
v2
Comments
Dilan Shaminda 8-Aug-14 10:51am    
and the question is?any exception you are getting?
Sandeep2014 9-Aug-14 0:50am    
not any exceptions really pleaze try the code for me i think there is a problem in soap action i am using it is a default soap action but as per the tutorial its deffrent site.
Sandeep2014 9-Aug-14 1:22am    
could you please tell what should be the soap action should i use in this case where the default namespace is showing tempuri.org?
Dilan Shaminda 9-Aug-14 1:45am    
see my answer :-)

1 solution

you are missing this line.
Java
envelope.dotNet = true;


your webservice has an issue..When invoke button is pressed it shows this error message

This page contains the following errors:
error on line 1 at column 37: Extra content at the end of the document
Below is a rendering of the page up to the first error.
 
Share this answer
 
v4
Comments
Sandeep2014 9-Aug-14 1:51am    
the webservice is online my friend and not on localhost and after applying only tempuri.org in soap action it is not working
Sandeep2014 9-Aug-14 1:52am    
here is the URL for the webservice u can check

http://www.namastii.co.in/Service.asmx
Dilan Shaminda 9-Aug-14 2:04am    
sorry my bad.I have updated the answer
Sandeep2014 9-Aug-14 2:05am    
where is answer
Dilan Shaminda 9-Aug-14 2:09am    
XML Parsing Error: no element found

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900