koush / ion

Android Asynchronous Networking and Image Loading
Other
6.29k stars 1.03k forks source link

Saving a prepared ION request without executing #590

Open stratbasher opened 9 years ago

stratbasher commented 9 years ago

Hello,

I'm trying to create my own FutureCallback function that will run allow me to run a callback and automatically have the custom callback check the response. If the response matches a certain requirement, another request is made, then the ORIGINAL request is made again.

Basically, make a request, check if the response is valid. If not, the user is not logged in. Run the login function using credentials in SharedPreferences, then when they are logged in, re-run the original callback.

Currently, I am trying to pass in the ResponseFuture as an argument to my new callback function Like this:

In my fragment

    ResponseFuture<JsonArray> rf = Ion
            .with(getActivity().getApplicationContext())
            .load("URLHERE")
            .setMultipartParameter("employeeid", employeeid)
            .asJsonArray();

            rf.setCallback(new LoginSafeFutureCallback<JsonArray>(getActivity().getApplicationContext(), rf) {

                @Override
                public void safeCallback(Exception e, JsonArray result) {
                    super.safeCallback(e, result);

                    //Do other stuff here

My LoginSafeFutureCallback class

public LoginSafeFutureCallback(Context context, ResponseFuture<T> responseFuture) {
    this.context = context;
    this.rf = responseFuture;
}

@Override
public void onCompleted(Exception e, T result) {
    if (e != null) {
        Log.w("error", e.toString());
    }

    if (result == null || result.equals("")) {
        try {
            Log.w("error", "Trying to get jsonobjectresult");
            JsonObject jsonObjectResult = Ion
                    .with(context)
                    .load("LOGIN_URL_HERE")
                    .setMultipartParameter("username", PreferenceManager.getDefaultSharedPreferences(context).getString("username", ""))
                    .setMultipartParameter("password", PreferenceManager.getDefaultSharedPreferences(context).getString("password", ""))
                    .asJsonObject()
                    .get();

            if (jsonObjectResult.get("status").getAsString().equals("Failed")) {
                Toast.makeText(context, "Your credentials are incorrect. Please log in.", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(context, LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(intent);
            }

            Response<T> reply = new Response<>(null, null, null, null, null);

            try {
                reply = rf.withResponse().get();
            } catch (Exception lsfce) {
                Log.w("error", lsfce.toString());
            }

            try {
                safeCallback(reply.getException(), reply.getResult());
                System.out.println("Exception: " + reply.getException().toString());
                System.out.println("Result: " + reply.getResult());
            } catch (NullPointerException npe) {
                Log.w("error", npe.toString());
            }

        } catch (Exception ionException) {
            Log.w("error", ionException.toString());
        }
    } else {
        safeCallback(e, result);
    }
}

public void safeCallback(Exception e, T result) {
    Log.w("error", "safeCallbackCalled");
}

This doesn't seem to work, as the actual web request seems to be made during the asJsonArray() function.

Is there any way to repeat a request or save the full thing before executing?

koush commented 9 years ago

Maybe create a runnable or other sort of function that wraps the Ion call. Then run it when you need it.

stratbasher commented 9 years ago

The problem is that I need my LoginSafeFutureCallback to be used in a variety of other functions as well, all which have different request parameters and URLs. I'm not entirely sure as to how I would be able to organize the code to work that way.

What would you recommend for passing the function-wrapped Ion call through the LoginSafeFutureCallback?