robospice icon indicating copy to clipboard operation
robospice copied to clipboard

Robospice Cache not work with object type Response

Open freask opened this issue 9 years ago • 0 comments

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<Response> { @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;
}

freask avatar Oct 08 '15 22:10 freask