mardi 20 août 2019

I can't redirect to an activity based on the request that comes from the web server

I have a volley that POST a Mac address of the mobile to server and returns the respond in string "yes" weather the condition in backend satisfy, when i checking the variable stored the responds in if else the else condition is only working weather the condition is true or false. This are all in Splash screen.

this is a async task containing volley..MacAuth variable declared globally.

`private class BackgroundTask extends AsyncTask {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected Object doInBackground(Object[] params) {


        /*  Use this method to load background
         * data that your app needs. */

        try {
            Thread.sleep(3000);

            final String mac = "{" + "\"address\"" + ":" + "\"" + MAC + "\"" + "}";
            String URL = "http://.....";
            RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
            StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject objRes = new JSONObject(response);
                        MacAuth = objRes.getString("statusCode");
                        // Toast.makeText(getApplicationContext(), objRes.toString(), Toast.LENGTH_LONG).show();

                    } catch (JSONException e) {
                        //Log.e("TAG", "Error " + error.getMessage());
                        e.getStackTrace();
                       // Toast.makeText(getApplicationContext(), mac, Toast.LENGTH_LONG).show();
                    }
                    Log.i("macadresssented",mac);
                    Log.i("MAC", response);
                    Log.i("MacAuth",MacAuth);
                  //  Toast.makeText(getApplicationContext(), MacAuth, Toast.LENGTH_LONG).show();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    //   Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
                    Log.e("TAG", "Error " + error.getMessage());
                    Log.v("VOLLEY", error.toString());
                    error.printStackTrace();
                }
            }) {
                @Override
                public String getBodyContentType() {
                    return "application/json; charset=utf-8";
                }

                @Override
                public byte[] getBody() throws AuthFailureError {
                    try {
                        return mac == null ? null : mac.getBytes("utf-8");

                    } catch (UnsupportedEncodingException uee) {
                        Log.v("Unsupported Encoding ", mac);
                        return null;
                    }
                }

            };

            requestQueue.add(stringRequest);
            if(MacAuth.equals("yes")) {Log.i("checkifno", MacAuth);
                intent = new Intent(Splash_Screen.this, MainActivity.class);
                startActivity(intent);
                finish();


            } else {Log.i("checkifyes", MacAuth);
                intent = new Intent(Splash_Screen.this, Error_Mac_Authentication.class);
                startActivity(intent);
                finish();

            }
            return params;

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute (Object o){
        super.onPostExecute(o);
    }
}

}`

this is the logcat output

I/macadresssented: {"address":"36:4E:07:GD:4B:90"}

I/MAC: {"statusCode":"yes"}

I/MacAuth: yes

Aucun commentaire:

Enregistrer un commentaire