Advertisement
Guest User

Untitled

a guest
Apr 9th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.20 KB | None | 0 0
  1. package com.example.alexa.androidprojectfinal;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.os.AsyncTask;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.EditText;
  12. import android.widget.Toast;
  13. import com.example.alexa.androidprojectfinal.Classes.Login;
  14. import org.json.JSONException;
  15. import org.json.JSONObject;
  16. import java.io.BufferedReader;
  17. import java.io.BufferedWriter;
  18. import java.io.InputStreamReader;
  19. import java.io.OutputStream;
  20. import java.io.OutputStreamWriter;
  21. import java.net.HttpURLConnection;
  22. import java.net.URL;
  23. import java.net.URLEncoder;
  24. import java.util.Iterator;
  25. import javax.net.ssl.HttpsURLConnection;
  26.  
  27.  
  28. public class LoginActivity extends AppCompatActivity {
  29.  
  30.     private EditText editTextUsername;
  31.     private EditText editTextPassword;
  32.     private String username;
  33.     private String password;
  34.     public static final String PREF_LOGIN = "Pref_login";
  35.     SharedPreferences preferences;
  36.     SharedPreferences.Editor editor;
  37.  
  38.     @Override
  39.     protected void onCreate(Bundle savedInstanceState) {
  40.         super.onCreate(savedInstanceState);
  41.         setContentView(R.layout.activity_login);
  42.  
  43.         editTextUsername = (EditText) findViewById(R.id.editTextUsername);
  44.         editTextPassword = (EditText) findViewById(R.id.editTextPassword);
  45.  
  46.         preferences = getSharedPreferences(PREF_LOGIN, MODE_PRIVATE);
  47.     }
  48.  
  49.  
  50.     public void onClickLogin(View view) {
  51.  
  52.         username = editTextUsername.getText().toString();
  53.         password = editTextPassword.getText().toString();
  54.  
  55.         if (username.isEmpty() || password.isEmpty()) {
  56.  
  57.             Toast.makeText(getApplicationContext(), "Username ou Password por preencher!", Toast.LENGTH_LONG).show();
  58.         }
  59.         else {
  60.  
  61.             new SendPostRequest().execute(username, password);
  62.  
  63.         }
  64.     }
  65.  
  66.     public class SendPostRequest extends AsyncTask<String, Void, String> {
  67.  
  68.         //protected void onPreExecute(){}
  69.  
  70.         protected String doInBackground(String... arg0) {
  71.  
  72.             try {
  73.  
  74.                 URL url = new URL("https://apiautocar.andrevicente.me/login/login"); // here is your URL path
  75.  
  76.                 JSONObject postDataParams = new JSONObject();
  77.                 postDataParams.put("username", username);
  78.                 postDataParams.put("password", password);
  79.                 Log.e("params",postDataParams.toString());
  80.  
  81.                 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  82.                 conn.setReadTimeout(15000 /* milliseconds */);
  83.                 conn.setConnectTimeout(15000 /* milliseconds */);
  84.                 conn.setRequestMethod("POST");
  85.                 conn.setDoInput(true);
  86.                 conn.setDoOutput(true);
  87.  
  88.                 OutputStream os = conn.getOutputStream();
  89.                 BufferedWriter writer = new BufferedWriter(
  90.                         new OutputStreamWriter(os, "UTF-8"));
  91.                 writer.write(getPostDataString(postDataParams));
  92.  
  93.                 writer.flush();
  94.                 writer.close();
  95.                 os.close();
  96.  
  97.                 int responseCode=conn.getResponseCode();
  98.  
  99.                 if (responseCode == HttpsURLConnection.HTTP_OK) {
  100.  
  101.                     BufferedReader in=new BufferedReader(new
  102.                             InputStreamReader(
  103.                             conn.getInputStream()));
  104.  
  105.                     StringBuffer sb = new StringBuffer("");
  106.                     String line="";
  107.  
  108.                     while((line = in.readLine()) != null) {
  109.  
  110.                         sb.append(line);
  111.                         break;
  112.                     }
  113.  
  114.                     in.close();
  115.                     return sb.toString();
  116.  
  117.                 }
  118.                 else {
  119.  
  120.                     return new String("Username ou password erradas");
  121.                 }
  122.             }
  123.             catch(Exception e){
  124.                 return new String("Exception: " + e.getMessage());
  125.             }
  126.             //return "";
  127.         }
  128.  
  129.         @Override
  130.         protected void onPostExecute(String result) {
  131.  
  132.             Login login = null;
  133.             try {
  134.                 JSONObject loginObj = new JSONObject(result);
  135.                 String status = loginObj.getString("status");
  136.                 String message = loginObj.getString("message");
  137.                 int id = loginObj.getInt("id");
  138.                 String token = loginObj.getString("token");
  139.                 String tipo = loginObj.getString("tipo");
  140.                 int idp = loginObj.getInt("idp");
  141.  
  142.  
  143.  
  144.                 editor = preferences.edit();
  145.                 editor.putString("token", token);
  146.                 editor.putString("tipo", tipo);
  147.                 editor.putInt("idp", idp);
  148.                 editor.apply();
  149.  
  150.                 //login = new Login(status, message, id, token, tipo, idp);
  151.  
  152.                 if (tipo.equals("cliente")) {
  153.  
  154.                     Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  155.                     startActivity(intent);
  156.  
  157.                 }
  158.                 else {
  159.                     Intent intent = new Intent(LoginActivity.this, MainMecanicoActivity.class);
  160.                     startActivity(intent);
  161.                 }
  162.  
  163.             } catch (JSONException e) {
  164.                 e.printStackTrace();
  165.                 Toast.makeText(getApplicationContext(), "Erro de credenciais", Toast.LENGTH_SHORT).show();
  166.             }
  167.         }
  168.     }
  169.     public String getPostDataString(JSONObject params) throws Exception {
  170.  
  171.         StringBuilder result = new StringBuilder();
  172.         boolean first = true;
  173.  
  174.         Iterator<String> itr = params.keys();
  175.  
  176.         while(itr.hasNext()){
  177.  
  178.             String key= itr.next();
  179.             Object value = params.get(key);
  180.  
  181.             if (first)
  182.                 first = false;
  183.             else
  184.                 result.append("&");
  185.  
  186.             result.append(URLEncoder.encode(key, "UTF-8"));
  187.             result.append("=");
  188.             result.append(URLEncoder.encode(value.toString(), "UTF-8"));
  189.  
  190.         }
  191.         return result.toString();
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement