Advertisement
Guest User

Untitled

a guest
Jun 11th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.17 KB | None | 0 0
  1. package com.example.koba10s.tas_project;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13. import com.android.volley.AuthFailureError;
  14. import com.android.volley.NetworkError;
  15. import com.android.volley.NoConnectionError;
  16. import com.android.volley.ParseError;
  17. import com.android.volley.Request;
  18. import com.android.volley.RequestQueue;
  19. import com.android.volley.Response;
  20. import com.android.volley.ServerError;
  21. import com.android.volley.TimeoutError;
  22. import com.android.volley.VolleyError;
  23. import com.android.volley.toolbox.JsonObjectRequest;
  24. import com.android.volley.toolbox.StringRequest;
  25. import com.android.volley.toolbox.Volley;
  26.  
  27. import org.json.JSONException;
  28. import org.json.JSONObject;
  29.  
  30. public class LoginActivity extends Activity {
  31.  
  32.     private static String password;
  33.     private static String username;
  34.     RequestQueue queue;
  35.     String url;
  36.  
  37.     @Override
  38.     protected void onCreate(Bundle savedInstanceState) {
  39.         super.onCreate(savedInstanceState);
  40.         setContentView(R.layout.activity_login);
  41.         final EditText etUsername = (EditText) findViewById(R.id.etUsername);
  42.         final EditText etPassword = (EditText) findViewById(R.id.etPassword);
  43.         final Button btLogin = (Button) findViewById(R.id.btLogin);
  44.         /*final TextView tvMessage = (TextView) findViewById(R.id.tvMessage);
  45.         final TextView tvMessage2 = (TextView) findViewById(R.id.tvMessage_2);*/
  46.         final JSONObject credentials = new JSONObject();
  47.         queue = Volley.newRequestQueue(this);
  48.         btLogin.setOnClickListener(new View.OnClickListener() {
  49.             @Override
  50.             public void onClick(View view) {
  51.                 username = etUsername.getText().toString();
  52.                 password = etPassword.getText().toString();
  53.                 url = "http://vps487563.ovh.net:8080/api/v1/sessions";
  54.                 if (etUsername.getText().toString().matches("") || etPassword.getText().toString().matches("")) {
  55.                     Toast.makeText(LoginActivity.this, "Username or password cannot be empty!", Toast.LENGTH_SHORT).show();
  56.                 }
  57.  
  58.                 try {
  59.                     credentials.put("username", username);
  60.                     credentials.put("password", password);
  61.                 } catch (JSONException e) {
  62.                     e.printStackTrace();
  63.                 }
  64.  
  65.                 // Request a string response from the provided URL.
  66.                 MetaRequest jsObjRequest = new MetaRequest(Request.Method.POST, url, credentials,
  67.                         new Response.Listener<JSONObject>() {
  68.                             @Override
  69.                             public void onResponse(JSONObject response) {
  70.                                 //tvMessage.setText(response.toString());
  71.                                 // On response DOOO WHAT?.
  72.                                 /*try {
  73.                                     tvMessage2.setText(response.getJSONObject("headers").get("set-cookie").toString());
  74.                                 } catch (JSONException e) {
  75.                                     e.printStackTrace();
  76.                                 }*/
  77.  
  78.                                 Intent intent = new Intent(LoginActivity.this, UserAreaActivity.class);
  79.                                 intent.putExtra("SESSION_USERNAME", username);
  80.                                 try {
  81.                                     intent.putExtra("SESSION_COOKIE", response.getJSONObject("headers").get("set-cookie").toString());
  82.                                 } catch (JSONException e) {
  83.                                     e.printStackTrace();
  84.                                 }
  85.                                 startActivity(intent);
  86.                                 finish();
  87.                             }
  88.                         }, new Response.ErrorListener() {
  89.                     @Override
  90.                     public void onErrorResponse(VolleyError error) {
  91.                         //tvMessage.setText(error.toString());
  92.                         if (error instanceof AuthFailureError) {
  93.                             Toast.makeText(LoginActivity.this, "Incorrect username or password!", Toast.LENGTH_SHORT).show();
  94.                         }
  95.                     }
  96.  
  97.                 });
  98.                 // Add the request to the RequestQueue.
  99.                 queue.add(jsObjRequest);
  100.  
  101.                 /* if(etUsername.getText().toString().matches(user1.getUsername()) && etPassword.getText().toString().matches(user1.getPassword())) {
  102.                     username = etUsername.getText().toString();
  103.                     Intent intent = new Intent(LoginActivity.this, UserAreaActivity.class);
  104.                     startActivity(intent);
  105.                     finish();
  106.                     */ /*
  107.                 }
  108.                 else {
  109.                     Toast.makeText(LoginActivity.this, "Incorret password or username", Toast.LENGTH_SHORT).show();
  110.                 } */
  111.             }
  112.         });
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement