Guest User

Untitled

a guest
Apr 26th, 2018
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.82 KB | None | 0 0
  1. package com.example.sigrundish.actio;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.TextView;
  10.  
  11. import com.android.volley.Request;
  12. import com.android.volley.RequestQueue;
  13. import com.android.volley.Response;
  14. import com.android.volley.VolleyError;
  15. import com.android.volley.toolbox.JsonObjectRequest;
  16. import com.android.volley.toolbox.Volley;
  17.  
  18. import org.json.JSONArray;
  19. import org.json.JSONException;
  20. import org.json.JSONObject;
  21.  
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24.  
  25. public class LoginActivity extends AppCompatActivity {
  26.  
  27.     @Override
  28.     protected void onCreate(Bundle savedInstanceState) {
  29.         super.onCreate(savedInstanceState);
  30.         setContentView(R.layout.activity_login);
  31.  
  32.         final TextView tvLoginValidation = (TextView) findViewById(R.id.tvLoginValidation);
  33.         final EditText etUsername = (EditText) findViewById(R.id.etUsername);
  34.         final EditText etPassword = (EditText) findViewById(R.id.etPassword);
  35.         final Button bLogin = (Button) findViewById(R.id.bLogin);
  36.         final TextView registerLink = (TextView) findViewById(R.id.tvRegisterHere);
  37.  
  38.         final RequestQueue queue = Volley.newRequestQueue(this);
  39.         final String url = "https://actio-server.herokuapp.com/login";
  40.  
  41.         queue.start();
  42.         bLogin.setOnClickListener(new View.OnClickListener() {
  43.             @Override
  44.             public void onClick(View view) {
  45.  
  46.                 HashMap<String, String> params = new HashMap<String,String>();
  47.                 params.put("username", etUsername.getText().toString()); // the entered data as the body.
  48.                 params.put("password", etPassword.getText().toString()); // the entered data as the body.
  49.                 final String username = params.get("username");
  50.                 JsonObjectRequest jsObjRequest = new
  51.                         JsonObjectRequest(Request.Method.POST,
  52.                         url,
  53.                         new JSONObject(params),
  54.                         new Response.Listener<JSONObject>() {
  55.                             @Override
  56.                             public void onResponse(JSONObject response) {
  57.                                 try {
  58.                                     if(response.getBoolean("login")){
  59.                                         JSONObject jsonUser = response.getJSONObject("data");
  60.                                         User user = new User();
  61.                                         user.setId(jsonUser.getInt("id"));
  62.                                         user.setUsername(jsonUser.getString("username"));
  63.                                         user.setPassword(jsonUser.getString("password"));
  64.                                         user.setName(jsonUser.getString("name"));
  65.                                         user.setAge(jsonUser.getString("age"));
  66.                                         Intent userAreaIntent = new Intent(LoginActivity.this, UserAreaActivity.class);
  67.                                         //Send the user to next activity.
  68.                                         userAreaIntent.putExtra("user", user);
  69.                                         LoginActivity.this.startActivity(userAreaIntent);
  70.                                     }
  71.                                     else{
  72.                                         JSONArray jsonValidation = response.getJSONArray("validation");
  73.                                         if (jsonValidation != null) {
  74.                                             ArrayList<String> validationArray = new ArrayList<>();
  75.                                             JSONObject json = jsonValidation.getJSONObject(0);
  76.                                             tvLoginValidation.setText(json.getString("error"));
  77.                                         }
  78.                                     }
  79.                                 } catch (JSONException e) {
  80.                                     e.printStackTrace();
  81.                                 }
  82.                             }
  83.                         }, new Response.ErrorListener() {
  84.                     @Override
  85.                     public void onErrorResponse(VolleyError error) {
  86.                         // DisplayText.setText("That didn't work!");
  87.                     }
  88.                 });
  89.                 queue.add(jsObjRequest);
  90.  
  91.             }
  92.         });
  93.  
  94.         registerLink.setOnClickListener(new View.OnClickListener() {
  95.             @Override
  96.             public void onClick(View view) {
  97.  
  98.                 Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
  99.                 LoginActivity.this.startActivity(registerIntent);
  100.                 finish();
  101.             }
  102.         });
  103.     }
  104. }
Add Comment
Please, Sign In to add comment