1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
package com.ngser.opa.qrcode;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Created by Opa on 30/09/2016.
*/
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
private EditText user, pass;
private TextView loginErrorMsg;
private Button bLogin;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
user = (EditText)findViewById(R.id.edi_numero);
pass = (EditText)findViewById(R.id.edit_code);
loginErrorMsg = (TextView) findViewById(R.id.ErrorMsg);
bLogin = (Button)findViewById(R.id.buton_login);
bLogin.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.buton_login:
new AttemptLogin().execute();
// here we have used, switch case, because on login activity you may
// also want to show registration button, so if the user is new ! we can go the
// registration activity , other than this we could also do this without switch
// case.
default: break;
}
}
public class AttemptLogin extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LoginActivity.this);
pDialog.setTitle("PayMoney Connexion");
pDialog.setMessage("Authentification...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// here Check for success tag
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
String convertMd5 = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(password.getBytes(), 0, pass.length());
convertMd5 = new BigInteger(1, md.digest()).toString(16);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(LoginActivity.class.getName()).log(Level.SEVERE, null, ex);
}
String lien = "http://localhost:8585/Json/webresources/compte/numero/"+username+"/"+convertMd5;
List<NameValuePair> params = new ArrayList <NameValuePair>();
params.add(new BasicNameValuePair("lien", lien));
Log.d("request!", "starting");
JSONObject json = jsonParser.makeHttpRequest(lien, "GET", params);
// checking log for json response
Log.d("Login attempt", json.toString());
// success tag for json
//success = json.getInt(TAG_SUCCESS);
if (json.toString().isEmpty()) {
Log.d("Successfully Login!", json.toString());
Intent ii = new Intent(LoginActivity.this, MainActivity.class);
finish();
// this finish() method is used to tell android os that we are done with current
// activity now! Moving to other activity
startActivity(ii);
Toast.makeText(LoginActivity.this, "Vous etes connecté", Toast.LENGTH_SHORT).show();
}else{
pDialog.dismiss();
loginErrorMsg.setText("Incorrect username/password");
}
return null;
}
/**
* Once the background process is done we need to Dismiss the progress dialog asap
* **/
protected void onPostExecute(String message) {
pDialog.dismiss();
}
}
} |
Partager