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
| public class LoginActivity extends Activity {
private EditText username,password;
private Button btnlogin;
private RequestQueue requestQueue;
private static final String URL = "http://10.0.2.2/database_connection/user_control.php";
private StringRequest request;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
btnlogin = (Button) findViewById(R.id.btnloginorregister);
requestQueue = Volley.newRequestQueue(this);
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
if(jsonObject.names().get(0).equals("success")){
Toast.makeText(getApplicationContext(),"SUCCESS "+jsonObject.getString("success"),Toast.LENGTH_LONG).show();
Intent i=new Intent(getApplicationContext(),MainScreenActivity.class);
}else {
Toast.makeText(getApplicationContext(), "Error" +jsonObject.getString("error"), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String,String> hashMap = new HashMap<String, String>();
hashMap.put("username",username.getText().toString());
hashMap.put("password",password.getText().toString());
return hashMap;
}
};
requestQueue.add(request);
}
});
}
} |
Partager