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
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 {
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);
userNameET = (EditText) findViewById(R.id.editText1);
passWordET = (EditText) findViewById(R.id.editText2);
statusTV = (TextView) findViewById(R.id.tv_result);
b = (Button) findViewById(R.id.button1);
webservicePG = (ProgressBar) findViewById(R.id.progressBar1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
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("");
AsyncCallWS task = new AsyncCallWS();
task.execute();
}
else{
statusTV.setText("Please enter Password");
}
} else {
statusTV.setText("Please enter Username");
}
}
});
}
private class AsyncCallWS extends AsyncTask<string,void,void> {
@Override
protected Void doInBackground(String... params) {
loginStatus = WebService.invokeLoginWS(editTextUsername,editTextPassword,"AuthenticateUser");
return null;
}
@Override
protected void onPostExecute(Void result) {
webservicePG.setVisibility(View.INVISIBLE);
Intent intObj = new Intent(CheckDNLoginActivity.this,HomeActivity.class);
if(!errored){
if(loginStatus){
startActivity(intObj);
}else{
statusTV.setText("Login Failed, try again");
}
}else{
statusTV.setText("Error occured in invoking webservice");
}
errored = false;
}
@Override
protected void onPreExecute() {
webservicePG.setVisibility(View.VISIBLE);
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
Here is my HomeActivity.java code:
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
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 {
private static String NAMESPACE = "http://tempuri.org/";
private static String URL = "http://namastii.co.in/Service.asmx";
private static String SOAP_ACTION = "http://tempuri.org/GetUserDetails";
public static boolean invokeLoginWS(String username,String passWD, String webMethName) {
boolean loginStatus = false;
SoapObject request = new SoapObject(NAMESPACE, webMethName);
PropertyInfo userName = new PropertyInfo();
PropertyInfo password = new PropertyInfo();
userName.setName("username");
userName.setValue(username);
userName.setType(String.class);
request.addProperty(userName);
password.setName("password");
password.setValue(passWD);
password.setType(String.class);
request.addProperty(password);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION+webMethName, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
loginStatus = Boolean.parseBoolean(response.toString());
} catch (Exception e) {
CheckDNLoginActivity.errored = true;
e.printStackTrace();
}
return loginStatus;
}
}