1

我正在用改造 2 重写 robospice 改造模块。
我遇到的问题有两种方法(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 接口的等价物是什么?

4

1 回答 1

2

我向 Jake Wharton 询问了这个问题,他回答说等效的是:
RequestBodyResponseBody

于 2016-01-03T09:04:27.047 回答