Transitioning from Using Jar to Dependency for Ion

429 views Asked by At

I originally had ion-1.3.8.jar in my libs directory in Android Studio, with compile files('libs/ion-1.3.8.jar') in my build.gradle file. I also had (and still have) androidasync-1.3.8.jar in my libs folder and an entry for it in build.gradle.

I want to get rid of the ion jar and instead use a dependency, but when I delete the jar and put compile 'com.koushikdutta.ion:ion:2.+ in my build.gradle file, I get an error ("cannot resolve method get(String...))" on the following line of code in one of my classes: result.getHeaders().get("USER-TOKEN");.

I'm guessing that line of code probably wasn't what it should have been all along, but it did work with Ion 1.3.8. I can't figure out what I should change it to, and though I can probably figure it out with some trial and error, I would like to hear from the community or Koush about this.

Also, Android Studio is warning me that "Avoid using + in version numbers; can lead to unpredictable and unrepeatable builds". Any thoughts about this?

Here's my code:

Ion.with(activity)
            .load(url)
            .setJsonObjectBody(loginObject)
            .asJsonObject()
            .withResponse()
            .setCallback(new FutureCallback<Response<JsonObject>>() {
                @Override
                public void onCompleted(Exception e, com.koushikdutta.ion.Response<JsonObject> result) {
                    String errorText = activity.getString(R.string.unable_to_login);
                    if (e != null) {
                        String errorMessage = e.getLocalizedMessage();
                        Log.e(TAG, errorMessage);
                        if (errorMessage.contains(activity.getString(R.string.unable_resolve_host))) {
                            errorText = activity.getString(R.string.no_internet);
                        }
                    }
                    else if (result != null) {
                        JsonElement reply = result.getResult().get(activity.getString(R.string.user));
                        if (reply != null) {
                            errorText = NO_ERROR;
                            SessionManager.setProfile(result.getResult());
                            String userToken = result.getHeaders().get(activity.getString(R.string.user_token));
                            SessionManager.createLoginSession(userToken);
                            if (SessionManager.isProfileComplete()) {
                                activity.startActivity(new Intent(activity, NavigationDrawerActivity.class));
                            }
                            else {
                                activity.startActivity(new Intent(activity, ProfileActivity.class));
                            }
                        }
                        else if (result.getResult().getAsJsonArray(activity.getString(R.string.errors)) != null) {
                            errorText = activity.getString(R.string.login_error);
                        }
                    }
                    else { // There was some other problem logging in
                        Log.e(TAG, "Unknown login problem");
                    }
                    requestCompleted.onRequestCompleted(errorText);
                }
            });
2

There are 2 answers

1
Christian Göllner On

On version 2.+ of ION the API probably changed.

If you want to use the old API that works with your code, change your dependency to: compile 'com.koushikdutta.ion:ion:1.3.8' It will also automatically add the Android Async 1.3.8 dependency for you.

The warning saying that you should avoid the '+' is because that way, gradle will always get the newest version it finds, where some APIs can change and then break your code.

7
koush On

The API changed slightly in 2. One of those was changing the names of a few methods around the ResponseFuture and RawHeaders classes. In your case, getHeaders() was simply renamed to headers().

The transition from 1.x to 2.x is fairly painless, but there are some breaking changes nonetheless.

If you don't want to deal with the breaking changes, and use the latest version of 1.x in gradle, do the following:

compile 'com.koushikdutta.ion:ion:1.+

There are no breaking changes between 1.x versions. Current 1.x version is 1.4.1 I believe.