2015-12-29 4 views
1

Je réécris le module de retrofit robospice avec mise à niveau 2.
le problème que j'ai sont 2 méthodes (SAVEDATA, readCacheDataFromFile):
Mise à niveau du module de retrofit 1 pour le rééquipement 2

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); 
     } 
    } 


} 


comment puis-je réécrire ces méthodes retrofit 2? Quel est l'équivalent des interfaces TypedOutput et TypedInput dans Retrofit 2?

Répondre

2

J'ai demandé Jake Wharton à ce sujet, et il a répondu que les équivalents sont:
RequestBody et responseBody