Monday 18 March 2013

Forgot password using HTTP GET method


import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ForgotPassword extends Activity {

private EditText etforgotPass;
private Button btnForgotPassSubmit;

private String strEmail;
private boolean isValidation;
private ProgressDialog m_ProgressDialog = null;
private String Result;
public static boolean IsExcption = false;
private int NewId, AffectedRows;
private boolean IsSuccess = false;
private String Message, DetailError, DetailMessage;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.forgotpassword);

setView();

btnForgotPassSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (validation()) {
// Hide Keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
strEmail = etforgotPass.getText().toString();

GetForgotPasswordTask forgotPasswordTask = new GetForgotPasswordTask();
forgotPasswordTask.execute();
}
}
});
}

private void setView() {
etforgotPass = (EditText) findViewById(R.id.etForgotEmailID);
btnForgotPassSubmit = (Button) findViewById(R.id.btnSubmitForgotPass);
}

private boolean validation() {
if (etforgotPass.getText().toString().equalsIgnoreCase("") || (etforgotPass.getText().toString().trim().length() == 0)) {
System.out.println("Please enter Email Address");
Toast.makeText(getApplicationContext(), "Please Enter Email Address", Toast.LENGTH_SHORT).show();
isValidation = false;
} else if (!emailValidation(etforgotPass.getText().toString())) {
isValidation = false;
Toast.makeText(getApplicationContext(), "Please Enter Valid Email Address", Toast.LENGTH_SHORT).show();
} else {
isValidation = true;
}
return isValidation;
}

public static boolean emailValidation(String emailstring) {
Pattern emailPattern = Pattern.compile("[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" + "\\@" + "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" + "(" + "\\."
+ "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" + ")+");
Matcher emailMatcher = emailPattern.matcher(emailstring);
return emailMatcher.matches();
}

private class GetForgotPasswordTask extends AsyncTask<Void, Void, String[]> {

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
m_ProgressDialog = ProgressDialog.show(ForgotPassword.this, "Please wait..", "Retrieving data ...", true);
m_ProgressDialog.setContentView(R.layout.progress);
m_ProgressDialog.show();

}

@Override
protected String[] doInBackground(Void... params) {
// Simulates a background job.
String mStrings[] = { "" };

try {
String Result = connection("http:/URL/forgotpassword/"
+ etforgotPass.getText().toString());
parseForgotPassword(Result); // HTTP POST METHOD
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
IsExcption = true;
}
// mListItems.addFirst("Added after refresh...");
return mStrings;
}

@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);

}

@Override
protected void onPostExecute(String[] result) {

// setLoginDetail();
m_ProgressDialog.dismiss();
System.out.println("ProgressBar Dismiss");
super.onPostExecute(result);

if (IsExcption) {
System.out.println("GlobelVeriable isException=" + IsExcption);
Toast.makeText(ForgotPassword.this, "Internet connection issue", Toast.LENGTH_SHORT).show();
// ----------------------------------------------------------------------------------------------------
/*
* LayoutInflater inflater = (LayoutInflater)
* getSystemService(Context.LAYOUT_INFLATER_SERVICE); View
* popuplayoutReject =
* inflater.inflate(R.layout.popup_force_close_error,
* (ViewGroup) findViewById(R.id.popup_element)); final
* PopupWindow popUpReject = new PopupWindow(popuplayoutReject,
* WindowManager.LayoutParams.WRAP_CONTENT,
* WindowManager.LayoutParams.WRAP_CONTENT, false); ImageButton
* btnPopupForceCloseOK; TextView tvForceErrorMessage =
* (TextView)
* popuplayoutReject.findViewById(R.id.tvForceErrorMessage);
* tvForceErrorMessage
* .setText(getResources().getString(R.string.
* noInternetConnection)); btnPopupForceCloseOK = (ImageButton)
* popuplayoutReject.findViewById(R.id.btnPopupForceCloseOK);
* btnPopupForceCloseOK.setOnClickListener(new OnClickListener()
* {

* @Override public void onClick(View v) {
* popUpReject.dismiss(); IsExcption = false;
* finishFromChild(getParent()); } });
* popUpReject.showAtLocation(popuplayoutReject, Gravity.CENTER,
* 0, 0);
*/
// ----------------------------------------------------------------------------------------------------
} else {
if (IsSuccess) {
// tvpopuptext.setVisibility(View.GONE);
// tvpopuptexterror.setText(Message);
// pw.showAtLocation(popuplayout, Gravity.CENTER, 0, 0);
Toast.makeText(getApplicationContext(), Message, Toast.LENGTH_SHORT).show();
finish();
} else {
Toast.makeText(getApplicationContext(), Message, Toast.LENGTH_SHORT).show();

}
}

}

}

private void parseForgotPassword(String jsonResponse) throws Exception {

JSONObject jObject = new JSONObject(jsonResponse);

NewId = jObject.getInt("NewId");
AffectedRows = jObject.getInt("AffectedRows");
IsSuccess = jObject.getBoolean("IsSuccess");
Message = jObject.getString("Message");
DetailMessage = jObject.getString("DetailMessages");

}

public static String connection(String URL) throws Exception {
String result_xml = null;
System.out.println("url-->is" + URL);
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(URL);
// request.addHeader("deviceId", deviceId);
ResponseHandler<String> handler = new BasicResponseHandler();
try {

result_xml = httpclient.execute(request, handler);
System.out.println("result of xml is------------------->" + result_xml); // print
// full
// xml
// here

} catch (ClientProtocolException e) {
e.printStackTrace();
IsExcption = true;
} catch (IOException e) {
e.printStackTrace();
IsExcption = true;
}

httpclient.getConnectionManager().shutdown();

return result_xml;

} // end of connection

}

No comments: