Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.koba10s.tas_project;
- import android.app.Activity;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- 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.NetworkError;
- import com.android.volley.NoConnectionError;
- import com.android.volley.ParseError;
- import com.android.volley.Request;
- import com.android.volley.RequestQueue;
- import com.android.volley.Response;
- import com.android.volley.ServerError;
- import com.android.volley.TimeoutError;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.JsonObjectRequest;
- import com.android.volley.toolbox.StringRequest;
- import com.android.volley.toolbox.Volley;
- import org.json.JSONException;
- import org.json.JSONObject;
- public class LoginActivity extends Activity {
- private static String password;
- private static String username;
- RequestQueue queue;
- String url;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- final EditText etUsername = (EditText) findViewById(R.id.etUsername);
- final EditText etPassword = (EditText) findViewById(R.id.etPassword);
- final Button btLogin = (Button) findViewById(R.id.btLogin);
- /*final TextView tvMessage = (TextView) findViewById(R.id.tvMessage);
- final TextView tvMessage2 = (TextView) findViewById(R.id.tvMessage_2);*/
- final JSONObject credentials = new JSONObject();
- queue = Volley.newRequestQueue(this);
- btLogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- username = etUsername.getText().toString();
- password = etPassword.getText().toString();
- url = "http://vps487563.ovh.net:8080/api/v1/sessions";
- if (etUsername.getText().toString().matches("") || etPassword.getText().toString().matches("")) {
- Toast.makeText(LoginActivity.this, "Username or password cannot be empty!", Toast.LENGTH_SHORT).show();
- }
- try {
- credentials.put("username", username);
- credentials.put("password", password);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- // Request a string response from the provided URL.
- MetaRequest jsObjRequest = new MetaRequest(Request.Method.POST, url, credentials,
- new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- //tvMessage.setText(response.toString());
- // On response DOOO WHAT?.
- /*try {
- tvMessage2.setText(response.getJSONObject("headers").get("set-cookie").toString());
- } catch (JSONException e) {
- e.printStackTrace();
- }*/
- Intent intent = new Intent(LoginActivity.this, UserAreaActivity.class);
- intent.putExtra("SESSION_USERNAME", username);
- try {
- intent.putExtra("SESSION_COOKIE", response.getJSONObject("headers").get("set-cookie").toString());
- } catch (JSONException e) {
- e.printStackTrace();
- }
- startActivity(intent);
- finish();
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- //tvMessage.setText(error.toString());
- if (error instanceof AuthFailureError) {
- Toast.makeText(LoginActivity.this, "Incorrect username or password!", Toast.LENGTH_SHORT).show();
- }
- }
- });
- // Add the request to the RequestQueue.
- queue.add(jsObjRequest);
- /* if(etUsername.getText().toString().matches(user1.getUsername()) && etPassword.getText().toString().matches(user1.getPassword())) {
- username = etUsername.getText().toString();
- Intent intent = new Intent(LoginActivity.this, UserAreaActivity.class);
- startActivity(intent);
- finish();
- */ /*
- }
- else {
- Toast.makeText(LoginActivity.this, "Incorret password or username", Toast.LENGTH_SHORT).show();
- } */
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement