package
bugworkshop.loginSystem;
import
androidx.appcompat.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
com.android.volley.AuthFailureError;
import
com.android.volley.Request;
import
com.android.volley.RequestQueue;
import
com.android.volley.Response;
import
com.android.volley.VolleyError;
import
com.android.volley.toolbox.StringRequest;
import
com.android.volley.toolbox.Volley;
import
org.json.JSONArray;
import
org.json.JSONException;
import
org.json.JSONObject;
import
java.util.HashMap;
import
java.util.Map;
public class
Login extends AppCompatActivity {
EditText password, email;
TextView hello;
Button signin;
TextView alert;
String pass,mail;
public RequestQueue mQueue;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
password =
findViewById(R.id.password);
email = findViewById(R.id.email);
hello = findViewById(R.id.hello);
signin = findViewById(R.id.signin);
signin.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
checklogin();
}
});
}
private void checklogin() {
mail =
email.getText().toString().trim();
pass
= password.getText().toString().trim();
String url =
"http://192.168.0.122/~test/efarm//login.php";
StringRequest sr = new
StringRequest(Request.Method.POST,url, new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
try {
JSONObject jsonobject =
new JSONObject(response);
String success =
jsonobject.getString("success");
JSONArray jsonarray =
jsonobject.getJSONArray("login");
JSONObject jo =
jsonarray.getJSONObject(0);
if(success.equals("1")) {
hello.setText(jo.getString("Name") + " Login OK");
}
else{
hello.setText("Wrong Username or Password");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void
onErrorResponse(VolleyError error) {
error.printStackTrace();
hello.setText("Error
Logging in check Internet Connection");
Toast.makeText(getApplicationContext(), "Error Logging in check
Internet Connection", Toast.LENGTH_LONG).show();
}
}){
@Override
protected
Map<String,String> getParams(){
HashMap<String,String>
param = new HashMap<String,String>();
param.put("email",mail);
param.put("password",pass);
return param;
}
@Override
public Map<String, String>
getHeaders() throws AuthFailureError {
Map<String,String>
params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
};
mQueue =
Volley.newRequestQueue(Login.this);
mQueue.add(sr);
}
}
|