为 RetrofitSpiceService 配置转换器

Configure Converter for RetrofitSpiceService

我正在使用 Retrofit 2.0 和 Robospice 库,它们包含在 gradle 文件中,如下所示:

compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.octo.android.robospice:robospice-retrofit:1.4.14'

我想使用 RetrofitGsonSpiceService 但是当我 运行 应用程序时,我得到 Suppressed: java.lang.ClassNotFoundException: retrofit.converter.GsonConverter

这是由于

@Override
    protected Converter createConverter() {
        return  new GsonConverter(new Gson());
    }

RetrofitGsonSpiceServiceclass的方法。

在 Retrofit 2.0 中,现在排除了默认转换器。

我的问题是,您是否知道配置 Robospice 以轻松使用 Retrofit 2.0 的简单方法,或者是否有预配置的 SpiceService 以使用新的 Retrofit 2.0?

不像以前那么简单了...

PS。我用的是 Moshi 解析器,但是用 GSON 应该是差不多的。

  1. 你应该扩展 SpiceService:

    public class SpiceNetworkService extends SpiceService {
    
    private Map<Class<?>, Object> retrofitInterfaceToServiceMap = new HashMap<>();
    protected List<Class<?>> retrofitInterfaceList = new ArrayList<>();
    
    @Override
    public void onCreate() {
        super.onCreate();
        addRetrofitInterface(WallpapersApi.class);
    }
    
    @SuppressWarnings("unchecked")
    protected <T> T getRetrofitService(Class<T> serviceClass) {
        T service = (T) retrofitInterfaceToServiceMap.get(serviceClass);
        if (service == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(getServerUrl())
                    .addConverterFactory(MoshiConverterFactory.create())
                    .build();
            service = retrofit.create(serviceClass);
            retrofitInterfaceToServiceMap.put(serviceClass, service);
        }
        return service;
    }
    
    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public void addRequest(CachedSpiceRequest<?> request, Set<RequestListener<?>> listRequestListener) {
        if (request.getSpiceRequest() instanceof RetrofitSpiceRequest) {
            RetrofitSpiceRequest retrofitSpiceRequest = (RetrofitSpiceRequest) request.getSpiceRequest();
            retrofitSpiceRequest.setService(getRetrofitService(retrofitSpiceRequest.getRetrofitedInterfaceClass()));
        }
        super.addRequest(request, listRequestListener);
    }
    
    public final List<Class<?>> getRetrofitInterfaceList() {
        return retrofitInterfaceList;
    }
    
    protected void addRetrofitInterface(Class<?> serviceClass) {
        retrofitInterfaceList.add(serviceClass);
    }
    
    @Override
    public CacheManager createCacheManager(Application application) throws CacheCreationException {
        CacheManager cacheManager = new CacheManager();
        cacheManager.addPersister(new MoshiPersisterFactory(application, getCacheFolder()));
        return cacheManager;
    }
    
    protected String getServerUrl() {
        return SpiceWallpapersRequest.API_URL;
    }
    
    public File getCacheFolder() {
        return null;
    }
    

    }

  2. 像这样实现 Persister 工厂:

    public class MoshiPersisterFactory extends InFileObjectPersisterFactory {
    public MoshiPersisterFactory(Application application) throws CacheCreationException {
        super(application);
    }
    
    public MoshiPersisterFactory(Application application, File cacheFolder) throws CacheCreationException {
        super(application, cacheFolder);
    }
    
    public MoshiPersisterFactory(Application application, List<Class<?>> listHandledClasses) throws CacheCreationException {
        super(application, listHandledClasses);
    }
    
    public MoshiPersisterFactory(Application application, List<Class<?>> listHandledClasses, File cacheFolder) throws CacheCreationException {
        super(application, listHandledClasses, cacheFolder);
    }
    
    @Override
    public <T> InFileObjectPersister<T> createInFileObjectPersister(Class<T> clazz, File cacheFolder) throws CacheCreationException {
        return new MoshiPersister<>(getApplication(), clazz, cacheFolder);
    }
    

    }

  3. 最后扩展 InFileObjectPersister:

    public class MoshiPersister 扩展 InFileObjectPersister { public MoshiPersister(Application application, Class clazz) throws CacheCreationException { 超级(应用程序,clazz); }

    public MoshiPersister(Application application, Class<T> clazz, File cacheFolder) throws CacheCreationException {
        super(application, clazz, cacheFolder);
    }
    
    @Override
    protected T readCacheDataFromFile(File file) throws CacheLoadingException {
        BufferedSource sink = null;
    
        try {
            InputStream fileInputStream = new FileInputStream(file);
            sink = Okio.buffer(Okio.source(fileInputStream));
    
            Moshi moshi = new Moshi.Builder().build();
            JsonAdapter<T> jsonAdapter = moshi.adapter(getHandledClass());
    
    
            return jsonAdapter.fromJson(sink);
        } catch (FileNotFoundException e) {
            // Should not occur (we test before if file exists)
            // Do not throw, file is not cached
            Ln.w("file " + file.getAbsolutePath() + " does not exists", e);
            return null;
        } catch (Exception e) {
            throw new CacheLoadingException(e);
        } finally {
            try {
                if (sink != null) {
                    sink.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    @Override
    public T saveDataToCacheAndReturnData(final T data, final Object cacheKey) throws CacheSavingException {
        try {
            if (isAsyncSaveEnabled()) {
                Thread t = new Thread() {
                    @Override
                    public void run() {
                        try {
                            saveData(data, cacheKey);
                        } catch (IOException | CacheSavingException e) {
                            Ln.w("An error occured on saving request " + cacheKey + " data asynchronously");
                        }
                    }
                };
                t.start();
            } else {
                saveData(data, cacheKey);
            }
        } catch (CacheSavingException e) {
            throw e;
        } catch (Exception e) {
            throw new CacheSavingException(e);
        }
        return data;
    }
    
    private void saveData(T data, Object cacheKey) throws IOException, CacheSavingException {
        Moshi moshi = new Moshi.Builder().build();
        JsonAdapter<T> jsonAdapter = moshi.adapter(getHandledClass());
        BufferedSink sink = new Buffer();
        jsonAdapter.toJson(sink, data);
    
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(getCacheFile(cacheKey));
            sink.buffer().copyTo(out);
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
    

    }