Advertisement
Guest User

javaku

a guest
Sep 23rd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.73 KB | None | 0 0
  1. package com.kalviandev.applokasi;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.animation.AlphaAnimation;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.TextView;
  10.  
  11. import com.kalviandev.applokasi.helpers.RbHelper;
  12.  
  13. import org.json.JSONException;
  14. import org.json.JSONObject;
  15.  
  16. import java.io.IOException;
  17.  
  18. import okhttp3.Call;
  19. import okhttp3.Callback;
  20. import okhttp3.FormBody;
  21. import okhttp3.Request;
  22. import okhttp3.RequestBody;
  23. import okhttp3.Response;
  24.  
  25. public class LoginActivity extends BaseActivity {
  26.     private EditText txtUsername,txtPassword;
  27.     private TextView tvDaftar;
  28.     private Button btnLogin;
  29.  
  30.     @Override
  31.     protected void onCreate(Bundle savedInstanceState) {
  32.         super.onCreate(savedInstanceState);
  33.         setContentView(R.layout.activity_login);
  34.         setupView();
  35.     }
  36.  
  37.     private void setupView(){
  38.         //koneksikan Variable dgn id xml
  39.         txtUsername = (EditText) findViewById(R.id.txtUsername);
  40.         txtPassword = (EditText) findViewById(R.id.txtPass);
  41.         btnLogin = (Button) findViewById(R.id.btnlogin);
  42.         tvDaftar = (TextView) findViewById(R.id.tvDaftar);
  43.         //action saat text fi klik
  44.         tvDaftar.setOnClickListener(new View.OnClickListener() {
  45.             @Override
  46.             public void onClick(View view) {
  47.                 //untuk efek spt klik
  48.                 AlphaAnimation animation  = new AlphaAnimation(1F,0.7F);
  49.                 view.startAnimation(animation);
  50.  
  51.                 //pindahkan ke Halaman Daftar
  52.                 Intent i = new Intent(getBaseContext(),DaftarActivity.class);
  53.                 startActivity(i);
  54.                 finish();
  55.             }
  56.         });
  57.  
  58.         clickBtnLogin();
  59.     }
  60.  
  61.     public void clickBtnLogin(){
  62.         btnLogin.setOnClickListener(new View.OnClickListener() {
  63.             @Override
  64.             public void onClick(View view) {
  65.  
  66.                 ActionLogin();
  67.  
  68.             }
  69.         });
  70.     }
  71.  
  72.     public void ActionLogin(){
  73.  
  74.         txtPassword.setError(null);
  75.         txtUsername.setError(null);
  76.  
  77.         //lakukan proses pengecekan semua field sudah disi dengan benar
  78.         boolean cancel = false;
  79.         View focusView= null;
  80.  
  81.         if (RbHelper.isEmpty(txtUsername)){
  82.  
  83.             txtUsername.setError("Email harus disisi");
  84.             focusView  = txtUsername;
  85.             cancel = true;
  86.         }else if (RbHelper.isEmpty(txtPassword)){
  87.  
  88.         txtPassword.setError("Passwor Harus Diisi");
  89.         focusView = txtPassword;
  90.         cancel= true;
  91.         }
  92.  
  93.  
  94.         //cek apakah ada yang belum diisi
  95.         if (cancel){
  96.             focusView.requestFocus();
  97.         }else {
  98.  
  99.             String url = RbHelper.BASE_URL + "user/login";
  100.  
  101.             //siapkan parameter yang akan di kirim
  102.             RequestBody formbody = new FormBody.Builder()
  103.                     .add("f_email",txtUsername.getText().toString())
  104.                     .add("f_password",txtPassword.getText().toString())
  105.                     .build();
  106.  
  107.             //buat request ayang akan di kirim keserver
  108.             final Request request = new Request.Builder()
  109.                     .url(url)
  110.                     .post(formbody)
  111.                     .build();
  112.  
  113.  
  114.             //debuging data yang dikirimkan
  115.  
  116.             RbHelper.pre("url : "+url+",parameter"+RbHelper.bodyToString(formbody));
  117.  
  118.             //tampilkan loadingnya
  119.             showLoading();
  120.  
  121.             //kirimkan ke server
  122.             okhttp.newCall(request).enqueue(new Callback() {
  123.                 @Override
  124.                 public void onFailure(Call call, final IOException e) {
  125.                     runOnUiThread(new Runnable() {
  126.                         @Override
  127.                         public void run() {
  128.                             try{
  129.                                 hideLoading();
  130.                                 RbHelper.pesan(c,"Error get data" + e.getMessage());
  131.                             }catch (Exception ex){
  132.  
  133.  
  134.                             }
  135.                         }
  136.                     });
  137.                 }
  138.  
  139.                 @Override
  140.                 public void onResponse(Call call, Response response) throws IOException {
  141.                     runOnUiThread(new Runnable() {
  142.                         @Override
  143.                         public void run() {
  144.                             try {
  145.                                 hideLoading();
  146.                             }catch (Exception e){
  147.  
  148.                             }
  149.                         }
  150.                     });
  151.  
  152.                     if (!response.isSuccessful()){
  153.                         throw new IOException("Unexpected code"+response);
  154.                     }
  155.  
  156.                     //baca response dari server
  157.                     final String reponData = response.body().string();
  158.                     RbHelper.pre("respon "+reponData);
  159.  
  160.                     runOnUiThread(new Runnable() {
  161.                         @Override
  162.                         public void run() {
  163.                             try{
  164.                                 JSONObject json = new JSONObject(reponData);
  165.  
  166.                                 //cek resultnya
  167.                                 boolean hasil = json.getBoolean("result");
  168.                                 String msg = json.getString("msg");
  169.  
  170.                                 RbHelper.pesan(c,msg);
  171.  
  172.                                 if (hasil){
  173.  
  174.                                     //create sesion loginya
  175.                                     String token = json.getString("token");
  176.                                     JSONObject jsonObject = json.getJSONObject("data");
  177.  
  178.                                     sesi.createSession(token);
  179.                                     sesi.setNama(jsonObject.getString("user_nama"));
  180.                                     sesi.setEmail(jsonObject.getString("user_email"));
  181.                                     sesi.setIdUser(jsonObject.getString("id_user"));
  182.  
  183.                                     Intent i = new Intent(c,MainActivity.class);
  184.                                     startActivity(i);
  185.                                     finish();
  186.                                 }
  187.                             }catch (JSONException e){
  188.                                 e.printStackTrace();
  189.                                 RbHelper.pesan(c,"Error pasrsing Json" + e.getMessage());
  190.                             }catch (Exception e){
  191.                                 e.printStackTrace();
  192.                                 RbHelper.pesan(c,"Error get data" + e.getMessage());
  193.                             }
  194.                         }
  195.                     });
  196.  
  197.                 }
  198.             });
  199.  
  200.         }
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement