Skip to content
This repository has been archived by the owner on Jan 14, 2018. It is now read-only.

Robospice Cache not work with object type Response #440

Open
freask opened this issue Oct 8, 2015 · 0 comments
Open

Robospice Cache not work with object type Response #440

freask opened this issue Oct 8, 2015 · 0 comments

Comments

@freask
Copy link

freask commented Oct 8, 2015

I have requests with type Response. I need it, because the server can return object json type User (if is OK), but also it can return json object Error (if has http or other errors).
My requests work fine, where they loadDataFormNetwork, but if data in the cache - my app crashing.
And what important - app crashs inly on Lollipop 5.0 and higher.
If i change my code to use type User (without checking errors) - it works fine!
Please help!

My code:

====Activity=====
getSpiceManager().execute(oneUserRequest, "user_id_" + Global.USER_ID, DurationInMillis.ONE_WEEK, new UserRequestListener());

public final class UserRequestListener implements RequestListener {
@OverRide
public void onRequestFailure(SpiceException spiceException) {
Toast.makeText(context, R.string.no_connect, Toast.LENGTH_SHORT).show();
}
@OverRide
public void onRequestSuccess(final Response response) {
oneUserRequestFired = true;
TypedInput body = response.getBody();
Converter converter = new GsonConverter(new Gson());
try {
ApiError error = (ApiError) converter.fromBody(body, ApiError.class);
if (!error.check())
throw new ConversionException("not error");

            error.go(context);
        } catch (ConversionException e1) {
            try {
                User user = (User) converter.fromBody(body, User.class);
                if (user != null) {
                    ...........MY CODE................
                } else
                    throw new ConversionException("Ошибка парсинга");
            } catch (ConversionException e2) {
                Toast.makeText(context, e2.toString(), Toast.LENGTH_LONG).show();
            }
        }
    }
}

====REQUEST====
public class OneUserRequest extends RetrofitSpiceRequest<Response, WishesService> {
private final int user_id;

public OneUserRequest(int user_id) {
    super(Response.class, WishesService.class);
    this.user_id = user_id;
}
@Override
public Response loadDataFromNetwork() {
    return getService().user(Global.APP_KEY, user_id, Global.AUTH_TOKEN);
}

}

====Service and API=====
@get("/v1/user")
Response user(@query("app_key") String app_key, @query("id") int user_id, @query("access-token") String access_token);

@OverRide
public void onCreate() {
super.onCreate();
addRetrofitInterface(WishesService.class);
// Logging really causes the app to chug with this many requests
Ln.getConfig().setLoggingLevel(Log.VERBOSE);

}
@Override
public CacheManager createCacheManager(Application application) throws CacheCreationException {
    CacheManager manager = new CacheManager();
    // Order is important: by default, Retrofit persists all objects.
    manager.addPersister(new RetrofitObjectPersisterFactory(application, getConverter(), getCacheFolder()));
    return manager;
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant