Retrying Android Volley onClick not working

I am making an application that often uses volley.

Therefore, wherever I make a request, everywhere in the application, Volley tries to use what is set in the retry policy. If he returns to me with an error, I need to show the AlertDialogredo button (and the preferred button for Wifi settings)

When the retry button is pressed , it should try again, which, if it receives an error again, should again show the same AlertDialog with the repeat button.

Value, if there is no connection, AlertDialog should continue to operate after a timeout period indefinitely, while the user presses the redo button.

Volley works fine, but doesn't seem to do anything after clicking the snooze button. that is, when I call the same jsonObjectRequest addToRequestQueue a second time.

I can just clearly see that all print logs placed above and below addToRequestQueue () are executed when the redo button is clicked . But I don’t know why I am not getting any response or error after the second request has been added to the queue. Here is what I tried ...

SplashActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    checkConnectionRequest = new JsonObjectRequest(
        Request.Method.POST, 
        getString(R.string.api_root_path) + "/check_connection", 
        JsonProvider.getAnonymousRequestJson(SplashActivity.this), 
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                intent = new Intent(SplashActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            DialogInterface.OnClickListener onClickTryAgain = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    makeRequest(checkConnectionRequest);
                }
            };
            if(error instanceof TimeoutError) Alerts.timeoutErrorAlert(SplashActivity.this, onClickTryAgain);
            else if(error instanceof NoConnectionError) Alerts.internetConnectionErrorAlert(SplashActivity.this, onClickTryAgain);
            System.out.println("Response Error: " + error);
        }
    });
    makeRequest(checkConnectionRequest);

}

private void makeRequest(JsonObjectRequest jsonObjectRequest) {
    VolleyClass.getInstance(SplashActivity.this).addToRequestQueue(jsonObjectRequest);
}

Alertler.java

public class Alerts {


    public static void internetConnectionErrorAlert(final Context context, DialogInterface.OnClickListener onClickTryAgainButton) {
        String message = "Sometimes the internet gets a bit sleepy and takes a nap. Make sure its up and running then we'll give it another go";
        new AlertDialog.Builder(context)
                .setCancelable(false)
                .setIconAttribute(android.R.attr.alertDialogIcon)
                .setTitle("Network Error")
                .setMessage(message)
                .setPositiveButton("Try Again", onClickTryAgainButton)
                .setNegativeButton("Turn Internet On", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent i = new Intent(Settings.ACTION_SETTINGS);
                        ((Activity) context).startActivityForResult(i, 0);
                    }
                })
                .show();
    }

    public static void timeoutErrorAlert(Context context, DialogInterface.OnClickListener onClickTryAgainButton) {
        String message = "Are you connected to internet? We guess you aren't. Turn it on and we'll rock and roll!";
        new AlertDialog.Builder(context)
                .setCancelable(false)
                .setIconAttribute(android.R.attr.alertDialogIcon)
                .setTitle("Network Error")
                .setMessage(message)
                .setPositiveButton("Try Again", onClickTryAgainButton)
                .show();
    }

}

PS . I do not want to change the retry policy to retry as many times as I want. I need to make a request only when the Retry button is pressed while AlertDialog is pressed.

Initially, I thought that passing onclicklisteners to AlertDialog would cause problems, and I tried the same with fragments. Bad luck.

?


Update:

, . , , - - Busy Socket?

02-04 19:05:27.058 I/System.out: Response Error: com.android.volley.TimeoutError

[ 02-04 19:05:29.078    76:   76 D/         ]
Socket deconnection

[ 02-04 19:05:31.098    76:   76 D/         ]
Socket deconnection

02-04 19:05:31.582 I/System.out: Retrying...
02-04 19:05:31.582 I/System.out: Adding to request queue...!


[ 02-04 19:05:33.122    76:   76 D/         ]
Socket deconnection

[ 02-04 19:05:34.990    76:   76 D/         ]
Socket deconnection

[ 02-04 19:05:36.778    76:   76 D/         ]
Socket deconnection

[ 02-04 19:05:38.726    76:   76 D/         ]
Socket deconnection

2:

, - ,

RequestToQueue , . ,

, Volley , , .

+4
2

Volley DETROYS , - , , .

, resent

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    makeRequest();
}

private void makeRequest() {
    JsonObjectRequest checkConnectionRequest = new JsonObjectRequest(
        Request.Method.POST, 
        getString(R.string.api_root_path) + "/check_connection", 
        JsonProvider.getAnonymousRequestJson(SplashActivity.this), 
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                intent = new Intent(SplashActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            DialogInterface.OnClickListener onClickTryAgain = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    makeRequest();
                }
            };
            if(error instanceof TimeoutError) Alerts.timeoutErrorAlert(SplashActivity.this, onClickTryAgain);
            else if(error instanceof NoConnectionError) Alerts.internetConnectionErrorAlert(SplashActivity.this, onClickTryAgain);
            System.out.println("Response Error: " + error);
        }
    });
    VolleyClass.getInstance(SplashActivity.this).addToRequestQueue(jsonObjectRequest);
}
0

. , , , . .

.

+2

Source: https://habr.com/ru/post/1627351/


All Articles