Advertisement
Guest User

Forgot_user_2

a guest
Dec 12th, 2017
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.13 KB | None | 0 0
  1. package com.nudi.bkn.sapk;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Intent;
  5. import android.graphics.Color;
  6. import android.os.Bundle;
  7. import android.support.design.widget.Snackbar;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15.  
  16. import com.android.volley.AuthFailureError;
  17. import com.android.volley.DefaultRetryPolicy;
  18. import com.android.volley.Request;
  19. import com.android.volley.Response;
  20. import com.android.volley.VolleyError;
  21. import com.android.volley.toolbox.HurlStack;
  22. import com.android.volley.toolbox.StringRequest;
  23. import com.nusantaradigital.bkn.sapk.R;
  24.  
  25. import org.json.JSONException;
  26. import org.json.JSONObject;
  27.  
  28. import java.io.IOException;
  29. import java.io.InputStream;
  30. import java.net.HttpURLConnection;
  31. import java.net.URL;
  32. import java.security.KeyManagementException;
  33. import java.security.KeyStore;
  34. import java.security.KeyStoreException;
  35. import java.security.NoSuchAlgorithmException;
  36. import java.security.cert.Certificate;
  37. import java.security.cert.CertificateException;
  38. import java.security.cert.CertificateFactory;
  39. import java.security.cert.X509Certificate;
  40. import java.util.HashMap;
  41. import java.util.Map;
  42.  
  43. import javax.net.ssl.HostnameVerifier;
  44. import javax.net.ssl.HttpsURLConnection;
  45. import javax.net.ssl.SSLContext;
  46. import javax.net.ssl.SSLSession;
  47. import javax.net.ssl.SSLSocketFactory;
  48. import javax.net.ssl.TrustManager;
  49. import javax.net.ssl.TrustManagerFactory;
  50. import javax.net.ssl.X509TrustManager;
  51.  
  52. public class Forgot_user_two extends AppCompatActivity {
  53.  
  54.     private EditText forgot_jawaban_user1;
  55.     private Button btn_next_forgot_user2;
  56.     private String state_respon, pertanyaan;
  57.     private TextView forgot_pertanyaan_user;
  58.     private String forgot_user;
  59.     private HashMap<String, String> params;
  60.  
  61.     @Override
  62.     protected void onCreate(Bundle savedInstanceState) {
  63.         super.onCreate(savedInstanceState);
  64.         setContentView(R.layout.activity_forgot_user_two);
  65.  
  66.         forgot_jawaban_user1 = (EditText) findViewById(R.id.forgot_jawaban_user1);
  67.         forgot_pertanyaan_user = (TextView) findViewById(R.id.forgot_pertanyaan_user);
  68.  
  69.         Intent intent = getIntent();
  70.         Bundle bd = intent.getExtras();
  71.  
  72.         String getData = (String) bd.get("json");
  73.  
  74.         try {
  75.  
  76.             JSONObject jo = new JSONObject(getData);
  77.  
  78.  
  79.             if (jo.getString("code").equals("1")) {
  80.  
  81.                 state_respon = jo.getString("state");
  82.                 pertanyaan = jo.getString("pertanyaan");
  83.                 forgot_pertanyaan_user.setText(pertanyaan);
  84.  
  85.  
  86.                 Log.e("Masuk state 2 ",state_respon.toString() );
  87.  
  88.  
  89.             } else{
  90.  
  91.             }
  92.  
  93.         }
  94.         catch (JSONException e) {
  95.             // If an error occurs, this prints the error to the log
  96.             e.printStackTrace();
  97.             Log.e("error","Y");
  98.         }
  99.  
  100.         btn_next_forgot_user2 = (Button) findViewById(R.id.button_next_forgot_user2);
  101.         btn_next_forgot_user2.setOnClickListener(new View.OnClickListener() {
  102.             @Override
  103.             public void onClick(View v) {
  104.                 if (forgot_jawaban_user1.getText().toString().trim().equals("")){
  105.                     forgot_jawaban_user1.setError("Silakan Masukan Jawaban Anda!");
  106. //                    forgot_newPassword.setError("Silakan Masukan Password Baru Anda!");
  107.                 } else {
  108.  
  109.                     try {
  110.                         Simpan();
  111.                     } catch (CertificateException e) {
  112.                         e.printStackTrace();
  113.                     } catch (NoSuchAlgorithmException e) {
  114.                         e.printStackTrace();
  115.                     } catch (KeyStoreException e) {
  116.                         e.printStackTrace();
  117.                     } catch (IOException e) {
  118.                         e.printStackTrace();
  119.                     } catch (KeyManagementException e) {
  120.                         e.printStackTrace();
  121.                     }
  122. //                    Intent intent1 = new Intent(Forgot_user_two.this, Forgot_password_three.class);
  123. //                    startActivity(intent1);
  124.                 }
  125.  
  126.             }
  127.         });
  128.     }
  129.     private void Simpan() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, KeyManagementException {
  130.  
  131.         HurlStack hurlStack = new HurlStack() {
  132.             @Override
  133.             protected HttpURLConnection createConnection(URL url) throws IOException {
  134.                 HttpsURLConnection httpsURLConnection = (HttpsURLConnection) super.createConnection(url);
  135.                 try {
  136.                     //handleSSLHandshake();
  137.                     httpsURLConnection.setSSLSocketFactory(getSSLSocketFactory());
  138.                     httpsURLConnection.setHostnameVerifier(getHostnameVerifier());
  139.                 } catch (Exception e) {
  140.                     e.printStackTrace();
  141.                 }
  142.                 return httpsURLConnection;
  143.             }
  144.         };
  145.  
  146.         forgot_user = forgot_jawaban_user1.getText().toString().trim();
  147. //        final String forgotNewPassword = forgot_newPassword.getText().toString().trim();
  148.  
  149.         final ProgressDialog pd = new ProgressDialog(Forgot_user_two.this);
  150.         pd.setMessage("Proses...");
  151.         pd.show();
  152. //        public Map<String, String> getHeaders() throws AuthFailureError {
  153. //            HashMap<String, String> headers = new HashMap<>();
  154. //            headers.put("origin", "http://localhost:20000");
  155. //            headers.put("accept-encoding", "gzip, deflate");
  156. //            headers.put("accept-language", "en-US,en;q=0.8");
  157. //            headers.put("Content-Type", "application/x-www-form-urlencoded");
  158. //            headers.put("Accept","application/x-www-form-urlencoded");
  159. //                headers.put("Accept","application/json");
  160. //                headers.put("Content-Type", "application/json");
  161. //            Log.e("test header", headers.toString());
  162. //            return headers;
  163. //        }
  164.  
  165. //        @Override
  166. //        protected Map<String,String> getParams(){
  167. //            params = new HashMap<String, String>();
  168. //            Log.e("beneran state 1",state_respon);
  169. //            Log.e("URL", Config.API_FORGOT_USER2 );
  170. //            params.put("state",state_respon);
  171. //            params.put("jawaban",forgot_user);
  172. //                params.put("newPassword",forgotNewPassword);
  173.  
  174. //            return params;
  175. //        }
  176.         final StringRequest kobj = new StringRequest(Request.Method.POST, Config.API_FORGOT_USER2,
  177.                  new Response.Listener<String>() {
  178.             @Override
  179.             public void onResponse(String response) {
  180.                 Log.e("Respon","y");
  181.                 pd.dismiss();
  182.  
  183.                 try {
  184.                     JSONObject jobj = new JSONObject(response);
  185.                     if (jobj.getString("code").equals("1")){
  186.                         Log.e("Masuk bool","y");
  187.  
  188.                         String username = jobj.getString("username");
  189.  
  190. //                        SessionManager.statrState(Forgot_password_one.this,state.trim());
  191. //                        Intent responState = new Intent(Forgot_password_one.this,Forgot_password_two.class);
  192. //                        startActivity(responState);
  193. //                        finish();
  194.                         Log.e("state 1" , username.toString());
  195.  
  196.                         Intent good = new Intent(Forgot_user_two.this,Forgot_user_three.class);
  197. //                        good.putExtra("username",username);
  198.                         startActivity(good);
  199.                     }else{
  200.                         String error = jobj.getString("message") ;
  201.                         //Toast.makeText(getContext(),error, Toast.LENGTH_SHORT).show();
  202.  
  203.                         Snackbar notiferror = Snackbar.make(getWindow().getDecorView().getRootView(),error,Snackbar.LENGTH_SHORT);
  204.  
  205.                         // merubah warna text pesan error
  206.                         View errorView = notiferror.getView();
  207.                         TextView textView = (TextView) errorView.findViewById(android.support.design.R.id.snackbar_text);
  208.                         textView.setTextColor(Color.RED);
  209.                         notiferror.show();
  210.                     }
  211.                 } catch (JSONException e) {
  212.                     e.printStackTrace();
  213.                 }
  214.             }
  215.         }, new Response.ErrorListener(){
  216.             @Override
  217.             public void onErrorResponse(VolleyError error){
  218.                 Log.e("Respon","t");
  219.                 pd.dismiss();
  220.                 if (error.toString().equals("com.android.volley.TimeoutError")){
  221.                     Toast.makeText(Forgot_user_two.this,"Koneksi Server Gagal", Toast.LENGTH_SHORT).show();
  222.                 }
  223. //                else {
  224. ////                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
  225. //                    Toast.makeText(Forgot_user_two.this,"Koneksi Server Tidak Ada", Toast.LENGTH_SHORT).show();
  226. //                }
  227.                 Log.i("CEKLOGIN", error.toString());
  228.             }
  229.         })
  230.         {
  231.  
  232.             @Override
  233.             public Map<String, String> getHeaders() throws AuthFailureError {
  234.                 HashMap<String, String> headers = new HashMap<>();
  235.                 headers.put("origin", "http://localhost:20000");
  236.                 headers.put("accept-encoding", "gzip, deflate");
  237.                 headers.put("accept-language", "en-US,en;q=0.8");
  238.                 headers.put("Content-Type", "application/x-www-form-urlencoded");
  239.                 headers.put("Accept","application/x-www-form-urlencoded");
  240. //                headers.put("Accept","application/json");
  241. //                headers.put("Content-Type", "application/json");
  242.                 Log.e("test header", headers.toString());
  243.                 return headers;
  244.             }
  245.  
  246.             @Override
  247.             protected Map<String,String> getParams(){
  248.                 Map<String,String> params = new HashMap<String, String>();
  249.                 Log.e("beneran state 1",state_respon);
  250.                 Log.e("URL", Config.API_FORGOT_USER2 );
  251.                 params.put("state",state_respon);
  252.                 params.put("jawaban",forgot_user);
  253. //                params.put("newPassword",forgotNewPassword);
  254.  
  255.                 return params;
  256.             }
  257.             @Override
  258.             public Priority getPriority() {
  259.                 return Priority.HIGH;
  260.             }
  261.         };
  262.         kobj.setRetryPolicy(new DefaultRetryPolicy(
  263.                 20000,
  264.                 DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
  265.                 DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
  266.         VolleySingleton.getInstance(this).addToRequestQueue(kobj);
  267. //        Volley.newRequestQueue(this,hurlStack).add(kobj);
  268.     }
  269.     private HostnameVerifier getHostnameVerifier() {
  270.         return new HostnameVerifier() {
  271.             @Override
  272.             public boolean verify(String hostname, SSLSession session) {
  273.                 //return true; // verify always returns true, which could cause insecure network traffic due to trusting TLS/SSL server certificates for wrong hostnames
  274.                 HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
  275.                 //return hv.verify("localhost", session);
  276.                 return true;
  277.             }
  278.         };
  279.     }
  280.     private TrustManager[] getWrappedTrustManagers(TrustManager[] trustManagers) {
  281.         final X509TrustManager originalTrustManager = (X509TrustManager) trustManagers[0];
  282.         return new TrustManager[]{
  283.                 new X509TrustManager() {
  284.                     public X509Certificate[] getAcceptedIssuers() {
  285.                         return originalTrustManager.getAcceptedIssuers();
  286.                     }
  287.  
  288.                     public void checkClientTrusted(X509Certificate[] certs, String authType) {
  289.                         try {
  290.                             if (certs != null && certs.length > 0){
  291.                                 certs[0].checkValidity();
  292.                             } else {
  293.                                 originalTrustManager.checkClientTrusted(certs, authType);
  294.                             }
  295.                         } catch (CertificateException e) {
  296.                             Log.w("checkClientTrusted", e.toString());
  297.                         }
  298.                     }
  299.  
  300.                     public void checkServerTrusted(X509Certificate[] certs, String authType) {
  301.                         try {
  302.                             if (certs != null && certs.length > 0){
  303.                                 certs[0].checkValidity();
  304.                             } else {
  305.                                 originalTrustManager.checkServerTrusted(certs, authType);
  306.                             }
  307.                         } catch (CertificateException e) {
  308.                             Log.w("checkServerTrusted", e.toString());
  309.                         }
  310.                     }
  311.                 }
  312.         };
  313.     }
  314.  
  315.     private SSLSocketFactory getSSLSocketFactory()
  316.             throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, KeyManagementException {
  317.         CertificateFactory cf = CertificateFactory.getInstance("X.509");
  318.         InputStream caInput = getResources().openRawResource(R.raw.bkngoid1); // this cert file stored in \app\src\main\res\raw folder path
  319.  
  320.         Certificate ca = cf.generateCertificate(caInput);
  321.         caInput.close();
  322.  
  323.         KeyStore keyStore = KeyStore.getInstance("BKS");
  324.         keyStore.load(null, null);
  325.         keyStore.setCertificateEntry("ca", ca);
  326.  
  327.         String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
  328.         TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
  329.         tmf.init(keyStore);
  330.  
  331.         TrustManager[] wrappedTrustManagers = getWrappedTrustManagers(tmf.getTrustManagers());
  332.  
  333.         SSLContext sslContext = SSLContext.getInstance("TLS");
  334.         sslContext.init(null, wrappedTrustManagers, null);
  335.  
  336.         return sslContext.getSocketFactory();
  337.     }
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement