stephanenicolas / robospice

Repo of the Open Source Android library : RoboSpice. RoboSpice is a modular android library that makes writing asynchronous long running tasks easy. It is specialized in network requests, supports caching and offers REST requests out-of-the box using extension modules.
Apache License 2.0
2.95k stars 545 forks source link

Robospice Cache not work with object type Response #440

Open freask opened 8 years ago

freask commented 8 years ago

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, "userid" + 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;
}