Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.nudi.bkn.sapk;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- 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.DefaultRetryPolicy;
- import com.android.volley.Request;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.HurlStack;
- import com.android.volley.toolbox.StringRequest;
- import com.nusantaradigital.bkn.sapk.R;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.security.KeyManagementException;
- import java.security.KeyStore;
- import java.security.KeyStoreException;
- import java.security.NoSuchAlgorithmException;
- import java.security.cert.Certificate;
- import java.security.cert.CertificateException;
- import java.security.cert.CertificateFactory;
- import java.security.cert.X509Certificate;
- import java.util.HashMap;
- import java.util.Map;
- import javax.net.ssl.HostnameVerifier;
- import javax.net.ssl.HttpsURLConnection;
- import javax.net.ssl.SSLContext;
- import javax.net.ssl.SSLSession;
- import javax.net.ssl.SSLSocketFactory;
- import javax.net.ssl.TrustManager;
- import javax.net.ssl.TrustManagerFactory;
- import javax.net.ssl.X509TrustManager;
- public class Forgot_user_two extends AppCompatActivity {
- private EditText forgot_jawaban_user1;
- private Button btn_next_forgot_user2;
- private String state_respon, pertanyaan;
- private TextView forgot_pertanyaan_user;
- private String forgot_user;
- private HashMap<String, String> params;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_forgot_user_two);
- forgot_jawaban_user1 = (EditText) findViewById(R.id.forgot_jawaban_user1);
- forgot_pertanyaan_user = (TextView) findViewById(R.id.forgot_pertanyaan_user);
- Intent intent = getIntent();
- Bundle bd = intent.getExtras();
- String getData = (String) bd.get("json");
- try {
- JSONObject jo = new JSONObject(getData);
- if (jo.getString("code").equals("1")) {
- state_respon = jo.getString("state");
- pertanyaan = jo.getString("pertanyaan");
- forgot_pertanyaan_user.setText(pertanyaan);
- Log.e("Masuk state 2 ",state_respon.toString() );
- } else{
- }
- }
- catch (JSONException e) {
- // If an error occurs, this prints the error to the log
- e.printStackTrace();
- Log.e("error","Y");
- }
- btn_next_forgot_user2 = (Button) findViewById(R.id.button_next_forgot_user2);
- btn_next_forgot_user2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (forgot_jawaban_user1.getText().toString().trim().equals("")){
- forgot_jawaban_user1.setError("Silakan Masukan Jawaban Anda!");
- // forgot_newPassword.setError("Silakan Masukan Password Baru Anda!");
- } else {
- try {
- Simpan();
- } catch (CertificateException e) {
- e.printStackTrace();
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- } catch (KeyStoreException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (KeyManagementException e) {
- e.printStackTrace();
- }
- // Intent intent1 = new Intent(Forgot_user_two.this, Forgot_password_three.class);
- // startActivity(intent1);
- }
- }
- });
- }
- private void Simpan() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, KeyManagementException {
- HurlStack hurlStack = new HurlStack() {
- @Override
- protected HttpURLConnection createConnection(URL url) throws IOException {
- HttpsURLConnection httpsURLConnection = (HttpsURLConnection) super.createConnection(url);
- try {
- //handleSSLHandshake();
- httpsURLConnection.setSSLSocketFactory(getSSLSocketFactory());
- httpsURLConnection.setHostnameVerifier(getHostnameVerifier());
- } catch (Exception e) {
- e.printStackTrace();
- }
- return httpsURLConnection;
- }
- };
- forgot_user = forgot_jawaban_user1.getText().toString().trim();
- // final String forgotNewPassword = forgot_newPassword.getText().toString().trim();
- final ProgressDialog pd = new ProgressDialog(Forgot_user_two.this);
- pd.setMessage("Proses...");
- pd.show();
- // public Map<String, String> getHeaders() throws AuthFailureError {
- // HashMap<String, String> headers = new HashMap<>();
- // headers.put("origin", "http://localhost:20000");
- // headers.put("accept-encoding", "gzip, deflate");
- // headers.put("accept-language", "en-US,en;q=0.8");
- // headers.put("Content-Type", "application/x-www-form-urlencoded");
- // headers.put("Accept","application/x-www-form-urlencoded");
- // headers.put("Accept","application/json");
- // headers.put("Content-Type", "application/json");
- // Log.e("test header", headers.toString());
- // return headers;
- // }
- // @Override
- // protected Map<String,String> getParams(){
- // params = new HashMap<String, String>();
- // Log.e("beneran state 1",state_respon);
- // Log.e("URL", Config.API_FORGOT_USER2 );
- // params.put("state",state_respon);
- // params.put("jawaban",forgot_user);
- // params.put("newPassword",forgotNewPassword);
- // return params;
- // }
- final StringRequest kobj = new StringRequest(Request.Method.POST, Config.API_FORGOT_USER2,
- new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- Log.e("Respon","y");
- pd.dismiss();
- try {
- JSONObject jobj = new JSONObject(response);
- if (jobj.getString("code").equals("1")){
- Log.e("Masuk bool","y");
- String username = jobj.getString("username");
- // SessionManager.statrState(Forgot_password_one.this,state.trim());
- // Intent responState = new Intent(Forgot_password_one.this,Forgot_password_two.class);
- // startActivity(responState);
- // finish();
- Log.e("state 1" , username.toString());
- Intent good = new Intent(Forgot_user_two.this,Forgot_user_three.class);
- // good.putExtra("username",username);
- startActivity(good);
- }else{
- String error = jobj.getString("message") ;
- //Toast.makeText(getContext(),error, Toast.LENGTH_SHORT).show();
- Snackbar notiferror = Snackbar.make(getWindow().getDecorView().getRootView(),error,Snackbar.LENGTH_SHORT);
- // merubah warna text pesan error
- View errorView = notiferror.getView();
- TextView textView = (TextView) errorView.findViewById(android.support.design.R.id.snackbar_text);
- textView.setTextColor(Color.RED);
- notiferror.show();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener(){
- @Override
- public void onErrorResponse(VolleyError error){
- Log.e("Respon","t");
- pd.dismiss();
- if (error.toString().equals("com.android.volley.TimeoutError")){
- Toast.makeText(Forgot_user_two.this,"Koneksi Server Gagal", Toast.LENGTH_SHORT).show();
- }
- // else {
- //// Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
- // Toast.makeText(Forgot_user_two.this,"Koneksi Server Tidak Ada", Toast.LENGTH_SHORT).show();
- // }
- Log.i("CEKLOGIN", error.toString());
- }
- })
- {
- @Override
- public Map<String, String> getHeaders() throws AuthFailureError {
- HashMap<String, String> headers = new HashMap<>();
- headers.put("origin", "http://localhost:20000");
- headers.put("accept-encoding", "gzip, deflate");
- headers.put("accept-language", "en-US,en;q=0.8");
- headers.put("Content-Type", "application/x-www-form-urlencoded");
- headers.put("Accept","application/x-www-form-urlencoded");
- // headers.put("Accept","application/json");
- // headers.put("Content-Type", "application/json");
- Log.e("test header", headers.toString());
- return headers;
- }
- @Override
- protected Map<String,String> getParams(){
- Map<String,String> params = new HashMap<String, String>();
- Log.e("beneran state 1",state_respon);
- Log.e("URL", Config.API_FORGOT_USER2 );
- params.put("state",state_respon);
- params.put("jawaban",forgot_user);
- // params.put("newPassword",forgotNewPassword);
- return params;
- }
- @Override
- public Priority getPriority() {
- return Priority.HIGH;
- }
- };
- kobj.setRetryPolicy(new DefaultRetryPolicy(
- 20000,
- DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
- DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
- VolleySingleton.getInstance(this).addToRequestQueue(kobj);
- // Volley.newRequestQueue(this,hurlStack).add(kobj);
- }
- private HostnameVerifier getHostnameVerifier() {
- return new HostnameVerifier() {
- @Override
- public boolean verify(String hostname, SSLSession session) {
- //return true; // verify always returns true, which could cause insecure network traffic due to trusting TLS/SSL server certificates for wrong hostnames
- HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
- //return hv.verify("localhost", session);
- return true;
- }
- };
- }
- private TrustManager[] getWrappedTrustManagers(TrustManager[] trustManagers) {
- final X509TrustManager originalTrustManager = (X509TrustManager) trustManagers[0];
- return new TrustManager[]{
- new X509TrustManager() {
- public X509Certificate[] getAcceptedIssuers() {
- return originalTrustManager.getAcceptedIssuers();
- }
- public void checkClientTrusted(X509Certificate[] certs, String authType) {
- try {
- if (certs != null && certs.length > 0){
- certs[0].checkValidity();
- } else {
- originalTrustManager.checkClientTrusted(certs, authType);
- }
- } catch (CertificateException e) {
- Log.w("checkClientTrusted", e.toString());
- }
- }
- public void checkServerTrusted(X509Certificate[] certs, String authType) {
- try {
- if (certs != null && certs.length > 0){
- certs[0].checkValidity();
- } else {
- originalTrustManager.checkServerTrusted(certs, authType);
- }
- } catch (CertificateException e) {
- Log.w("checkServerTrusted", e.toString());
- }
- }
- }
- };
- }
- private SSLSocketFactory getSSLSocketFactory()
- throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, KeyManagementException {
- CertificateFactory cf = CertificateFactory.getInstance("X.509");
- InputStream caInput = getResources().openRawResource(R.raw.bkngoid1); // this cert file stored in \app\src\main\res\raw folder path
- Certificate ca = cf.generateCertificate(caInput);
- caInput.close();
- KeyStore keyStore = KeyStore.getInstance("BKS");
- keyStore.load(null, null);
- keyStore.setCertificateEntry("ca", ca);
- String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
- TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
- tmf.init(keyStore);
- TrustManager[] wrappedTrustManagers = getWrappedTrustManagers(tmf.getTrustManagers());
- SSLContext sslContext = SSLContext.getInstance("TLS");
- sslContext.init(null, wrappedTrustManagers, null);
- return sslContext.getSocketFactory();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement