我无法根据来自 Web 服务器的请求重定向到 activity

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

我有一个凌空 POST Mac 手机到服务器的地址和 returns 字符串中的响应“是”天气后端满足条件,当我检查变量存储在 if else else 条件中的响应仅在条件为真或假的情况下有效。这些都在启动画面中。

这是一个包含 volley..Mac全局声明的 Auth 变量的异步任务。

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);
        }
    }
}

这是logcat输出

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

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

I/MacAuth: yes

   try {
        JSONObject objRes = new JSONObject(response);
        MacAuth = objRes.getString("statusCode");
        // Toast.makeText(getApplicationContext(), objRes.toString(), 
        Toast.LENGTH_LONG).show();

        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();

        }

    } catch (JSONException e) {
        //Log.e("TAG", "Error " + error.getMessage());
        e.getStackTrace();
        // Toast.makeText(getApplicationContext(), mac, Toast.LENGTH_LONG).show();
    }

你可以在try-catch语句里面试试。

         StringRequest request = new StringRequest(Request.Method.POST, UrlsAvision.URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    String MacAuth =null ;
                    Intent intent;
                    try {
                        JSONObject objRes = new JSONObject(response);
                        MacAuth = objRes.getString("statusCode");
                        // Toast.makeText(getApplicationContext(), objRes.toString(), Toast.LENGTH_LONG).show();

                        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();

                        }

                    } catch (JSONException e) {
                        //Log.e("TAG", "Error " + error.getMessage());
                        e.getStackTrace();
                        // Toast.makeText(getApplicationContext(), mac, Toast.LENGTH_LONG).show();
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }){
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new Hashtable<>();
            params.put("yourParams", "");
            return params;
        }
    };

    request.setRetryPolicy(new DefaultRetryPolicy(10000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    AppWebService.getInstance(context).addToRequestQueue(request);

我将 if else 移至 onResponse。现在它工作正常。

public void onResponse(String response) {
                    try {
                        JSONObject objRes = new JSONObject(response);
                        MacAuth = objRes.getString("statusCode");
                        // Toast.makeText(getApplicationContext(), objRes.toString(), Toast.LENGTH_LONG).show();

                        if(MacAuth.equals("yes")) {
                            Log.i("checkMain", MacAuth);
                            intent = new Intent(Splash_Screen.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            Log.i("checkErr", MacAuth);
                            intent = new Intent(Splash_Screen.this, Error_Mac_Authentication.class);
                            startActivity(intent);
                            finish();
                        }

                    } 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();
                }