Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.alexa.androidprojectfinal;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.AsyncTask;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.example.alexa.androidprojectfinal.Classes.Login;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.util.Iterator;
- import javax.net.ssl.HttpsURLConnection;
- public class LoginActivity extends AppCompatActivity {
- private EditText editTextUsername;
- private EditText editTextPassword;
- private String username;
- private String password;
- public static final String PREF_LOGIN = "Pref_login";
- SharedPreferences preferences;
- SharedPreferences.Editor editor;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- editTextUsername = (EditText) findViewById(R.id.editTextUsername);
- editTextPassword = (EditText) findViewById(R.id.editTextPassword);
- preferences = getSharedPreferences(PREF_LOGIN, MODE_PRIVATE);
- }
- public void onClickLogin(View view) {
- username = editTextUsername.getText().toString();
- password = editTextPassword.getText().toString();
- if (username.isEmpty() || password.isEmpty()) {
- Toast.makeText(getApplicationContext(), "Username ou Password por preencher!", Toast.LENGTH_LONG).show();
- }
- else {
- new SendPostRequest().execute(username, password);
- }
- }
- public class SendPostRequest extends AsyncTask<String, Void, String> {
- //protected void onPreExecute(){}
- protected String doInBackground(String... arg0) {
- try {
- URL url = new URL("https://apiautocar.andrevicente.me/login/login"); // here is your URL path
- JSONObject postDataParams = new JSONObject();
- postDataParams.put("username", username);
- postDataParams.put("password", password);
- Log.e("params",postDataParams.toString());
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setReadTimeout(15000 /* milliseconds */);
- conn.setConnectTimeout(15000 /* milliseconds */);
- conn.setRequestMethod("POST");
- conn.setDoInput(true);
- conn.setDoOutput(true);
- OutputStream os = conn.getOutputStream();
- BufferedWriter writer = new BufferedWriter(
- new OutputStreamWriter(os, "UTF-8"));
- writer.write(getPostDataString(postDataParams));
- writer.flush();
- writer.close();
- os.close();
- int responseCode=conn.getResponseCode();
- if (responseCode == HttpsURLConnection.HTTP_OK) {
- BufferedReader in=new BufferedReader(new
- InputStreamReader(
- conn.getInputStream()));
- StringBuffer sb = new StringBuffer("");
- String line="";
- while((line = in.readLine()) != null) {
- sb.append(line);
- break;
- }
- in.close();
- return sb.toString();
- }
- else {
- return new String("Username ou password erradas");
- }
- }
- catch(Exception e){
- return new String("Exception: " + e.getMessage());
- }
- //return "";
- }
- @Override
- protected void onPostExecute(String result) {
- Login login = null;
- try {
- JSONObject loginObj = new JSONObject(result);
- String status = loginObj.getString("status");
- String message = loginObj.getString("message");
- int id = loginObj.getInt("id");
- String token = loginObj.getString("token");
- String tipo = loginObj.getString("tipo");
- int idp = loginObj.getInt("idp");
- editor = preferences.edit();
- editor.putString("token", token);
- editor.putString("tipo", tipo);
- editor.putInt("idp", idp);
- editor.apply();
- //login = new Login(status, message, id, token, tipo, idp);
- if (tipo.equals("cliente")) {
- Intent intent = new Intent(LoginActivity.this, MainActivity.class);
- startActivity(intent);
- }
- else {
- Intent intent = new Intent(LoginActivity.this, MainMecanicoActivity.class);
- startActivity(intent);
- }
- } catch (JSONException e) {
- e.printStackTrace();
- Toast.makeText(getApplicationContext(), "Erro de credenciais", Toast.LENGTH_SHORT).show();
- }
- }
- }
- public String getPostDataString(JSONObject params) throws Exception {
- StringBuilder result = new StringBuilder();
- boolean first = true;
- Iterator<String> itr = params.keys();
- while(itr.hasNext()){
- String key= itr.next();
- Object value = params.get(key);
- if (first)
- first = false;
- else
- result.append("&");
- result.append(URLEncoder.encode(key, "UTF-8"));
- result.append("=");
- result.append(URLEncoder.encode(value.toString(), "UTF-8"));
- }
- return result.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement