升级改造1模块进行改造2

时间:2015-12-29 10:11:11

标签: android retrofit robospice retrofit2

我正在通过改造2改写robospice改装模块 我遇到的问题是2个方法(saveData,readCacheDataFromFile):

import com.octo.android.robospice.persistence.exception.CacheCreationException;
import com.octo.android.robospice.persistence.exception.CacheLoadingException;
import com.octo.android.robospice.persistence.exception.CacheSavingException;
import com.octo.android.robospice.persistence.file.InFileObjectPersister;

public class RetrofitObjectPersister<T> extends InFileObjectPersister<T> {

    // ============================================================================================
    // ATTRIBUTES
    // ============================================================================================

    private final Converter converter;

    // ============================================================================================
    // CONSTRUCTOR
    // ============================================================================================

    public RetrofitObjectPersister(Application application, Converter converter, Class<T> clazz, File cacheFolder) throws CacheCreationException {
        super(application, clazz, cacheFolder);
        this.converter = converter;
    }

    public RetrofitObjectPersister(Application application, Converter converter, Class<T> clazz) throws CacheCreationException {
        this(application, converter, clazz, null);
    }

    // ============================================================================================
    // METHODS
    // ============================================================================================

    @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 e) {
                            Ln.e(e, "An error occured on saving request " + cacheKey + " data asynchronously");
                        } catch (CacheSavingException e) {
                            Ln.e(e, "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 {
        // transform the content in json to store it in the cache
        TypedOutput typedBytes = converter.toBody(data);
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(getCacheFile(cacheKey));
            typedBytes.writeTo(out);
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    protected T readCacheDataFromFile(File file) throws CacheLoadingException {
        InputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            final byte[] body = IOUtils.toByteArray(fileInputStream);
            TypedInput typedInput = new TypedInput() {

                @Override
                public String mimeType() {
                    return "application/json";
                }

                @Override
                public long length() {
                    return body.length;
                }

                @Override
                public InputStream in() throws IOException {
                    return new ByteArrayInputStream(body);
                }
            };
            return (T) converter.fromBody(typedInput, getHandledClass());
        } 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 {
            IOUtils.closeQuietly(fileInputStream);
        }
    }


}


我怎样才能在改造2中重写这些方法? 改造2中的TypedOutput和TypedInput接口相当于什么?

1 个答案:

答案 0 :(得分:2)

我问杰克沃顿这件事,他回答说等价物是:
RequestBody ResponseBody

相关问题