WEBSERVICE LOGIN EXAMPLE IN ANDROID
package com.imcq;
import java.io.IOException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
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;
import org.xmlpull.v1.XmlPullParserException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
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;
public class Login extends Activity {
Button login;
EditText username;
EditText password;
Activity mactivity;
@SuppressLint("SimpleDateFormat")@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
mactivity = this;
login = (Button) findViewById(R.id.login_btn_signin);
username = (EditText) findViewById(R.id.login_et_username);
password = (EditText) findViewById(R.id.login_et_password);
login.setOnClickListener(new OnClickListener() {@Override
public void onClick(View arg0) {
DownloadWebPageTask abc = new DownloadWebPageTask();
abc.execute();
}
});
}
protected boolean isOnline() {
ConnectivityManager connectivity = (ConnectivityManager) mactivity.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setTitle("User Name or Password is wrong")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
password.setText("");
}
}).create();
case 1:
return new AlertDialog.Builder(this)
.setTitle("Check your network connection....")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).create();
case 2:
return new AlertDialog.Builder(this)
.setTitle("Server Not Found......")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).create();
}
return null;
}
private final String METHOD_NAME = "StudentLogin";
private final String SOAP_ACTION = App_Variable.NAMESPACE + METHOD_NAME;
private String webResponse = "";
private class DownloadWebPageTask extends AsyncTask < String, Void, String > {
ProgressDialog progDailog;@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
progDailog = new ProgressDialog(mactivity);
progDailog.setMessage("Loading...");
progDailog.setIndeterminate(false);
progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progDailog.setCancelable(true);
progDailog.show();
}
@Override
protected String doInBackground(String...arg0) {
// TODO Auto-generated method stub
SoapObject request = new SoapObject(App_Variable.NAMESPACE,
METHOD_NAME);
PropertyInfo pro1 = new PropertyInfo();
pro1.setName("UserName");
pro1.setValue(username.getText().toString());
pro1.setType(String.class);
request.addProperty(pro1);
PropertyInfo pro2 = new PropertyInfo();
pro2.setName("Password");
pro2.setValue(password.getText().toString());
pro2.setType(String.class);
request.addProperty(pro2);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(
App_Variable.URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
SoapPrimitive response = null;
try {
response = (SoapPrimitive) envelope.getResponse();
webResponse = response.toString();
} catch (SoapFault e) {
e.printStackTrace();
} catch (Exception e) {
webResponse = "Error";
}
return webResponse;
}
@SuppressWarnings("deprecation")@Override
protected void onPostExecute(String result) {
progDailog.dismiss();
JSONArray jarray = null;
JSONObject jobject = null;
try {
if (webResponse != "[]" && webResponse != "Error") {
jarray = new JSONArray(webResponse);
jobject = jarray.getJSONObject(0);
App_Variable.StudentID = jobject.getInt("StudentID");
App_Variable.SemesterID = jobject.getInt("CurrentSemesterID");
App_Variable.UserName = jobject.getString("UserName").toString();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (webResponse == "Error") {
showDialog(2);
} else if (App_Variable.StudentID != 0) {
Intent in = new Intent(Login.this, Dashbord.class);
startActivity( in );
} else {
showDialog(0);
}
}
}
}
import java.io.IOException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
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;
import org.xmlpull.v1.XmlPullParserException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
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;
public class Login extends Activity {
Button login;
EditText username;
EditText password;
Activity mactivity;
@SuppressLint("SimpleDateFormat")@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
mactivity = this;
login = (Button) findViewById(R.id.login_btn_signin);
username = (EditText) findViewById(R.id.login_et_username);
password = (EditText) findViewById(R.id.login_et_password);
login.setOnClickListener(new OnClickListener() {@Override
public void onClick(View arg0) {
DownloadWebPageTask abc = new DownloadWebPageTask();
abc.execute();
}
});
}
protected boolean isOnline() {
ConnectivityManager connectivity = (ConnectivityManager) mactivity.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setTitle("User Name or Password is wrong")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
password.setText("");
}
}).create();
case 1:
return new AlertDialog.Builder(this)
.setTitle("Check your network connection....")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).create();
case 2:
return new AlertDialog.Builder(this)
.setTitle("Server Not Found......")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).create();
}
return null;
}
private final String METHOD_NAME = "StudentLogin";
private final String SOAP_ACTION = App_Variable.NAMESPACE + METHOD_NAME;
private String webResponse = "";
private class DownloadWebPageTask extends AsyncTask < String, Void, String > {
ProgressDialog progDailog;@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
progDailog = new ProgressDialog(mactivity);
progDailog.setMessage("Loading...");
progDailog.setIndeterminate(false);
progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progDailog.setCancelable(true);
progDailog.show();
}
@Override
protected String doInBackground(String...arg0) {
// TODO Auto-generated method stub
SoapObject request = new SoapObject(App_Variable.NAMESPACE,
METHOD_NAME);
PropertyInfo pro1 = new PropertyInfo();
pro1.setName("UserName");
pro1.setValue(username.getText().toString());
pro1.setType(String.class);
request.addProperty(pro1);
PropertyInfo pro2 = new PropertyInfo();
pro2.setName("Password");
pro2.setValue(password.getText().toString());
pro2.setType(String.class);
request.addProperty(pro2);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(
App_Variable.URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
SoapPrimitive response = null;
try {
response = (SoapPrimitive) envelope.getResponse();
webResponse = response.toString();
} catch (SoapFault e) {
e.printStackTrace();
} catch (Exception e) {
webResponse = "Error";
}
return webResponse;
}
@SuppressWarnings("deprecation")@Override
protected void onPostExecute(String result) {
progDailog.dismiss();
JSONArray jarray = null;
JSONObject jobject = null;
try {
if (webResponse != "[]" && webResponse != "Error") {
jarray = new JSONArray(webResponse);
jobject = jarray.getJSONObject(0);
App_Variable.StudentID = jobject.getInt("StudentID");
App_Variable.SemesterID = jobject.getInt("CurrentSemesterID");
App_Variable.UserName = jobject.getString("UserName").toString();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (webResponse == "Error") {
showDialog(2);
} else if (App_Variable.StudentID != 0) {
Intent in = new Intent(Login.this, Dashbord.class);
startActivity( in );
} else {
showDialog(0);
}
}
}
}


Comments
Post a Comment