Ejemplo n.º 1
0
 public JsonElement get(JsonObject object, String memberName) throws IOException {
   JsonElement element = object.get(memberName);
   if (element == null) {
     JsonObject links = object.getAsJsonObject("links");
     if (links != null && links.has(memberName)) {
       Request request =
           new Request.Builder()
               .url(HATCHET_BASE_URL + links.get(memberName).getAsString())
               .build();
       Log.d(TAG, "following link: " + request.urlString());
       Response response = mOkHttpClient.newCall(request).execute();
       if (!response.isSuccessful()) {
         throw new IOException(
             "API request with URL '"
                 + request.urlString()
                 + "' not successful. Code was "
                 + response.code());
       }
       element = GsonHelper.get().fromJson(response.body().charStream(), JsonElement.class);
     }
   }
   return element;
 }
Ejemplo n.º 2
0
  public Store() {
    RequestInterceptor requestInterceptor =
        new RequestInterceptor() {
          @Override
          public void intercept(RequestFacade request) {
            if (!NetworkUtils.isNetworkAvailable()) {
              int maxStale = 60 * 60 * 24 * 7; // tolerate 1-week stale
              request.addHeader("Cache-Control", "public, max-stale=" + maxStale);
            }
            request.addHeader("Content-type", "application/json; charset=utf-8");
          }
        };
    mOkHttpClient = new OkHttpClient();
    File cacheDir = new File(TomahawkApp.getContext().getCacheDir(), "responseCache");
    Cache cache = new Cache(cacheDir, 1024 * 1024 * 20);
    mOkHttpClient.setCache(cache);
    RestAdapter restAdapter =
        new RestAdapter.Builder()
            .setLogLevel(RestAdapter.LogLevel.BASIC)
            .setEndpoint(HATCHET_BASE_URL + HATCHET_API_VERSION)
            .setConverter(new GsonConverter(GsonHelper.get()))
            .setRequestInterceptor(requestInterceptor)
            .setClient(new OkClient(mOkHttpClient))
            .build();
    mHatchet = restAdapter.create(Hatchet.class);

    Executor httpExecutor =
        Executors.newCachedThreadPool(
            new ThreadFactory() {
              @Override
              public Thread newThread(final Runnable r) {
                return new Thread(
                    new Runnable() {
                      @Override
                      public void run() {
                        android.os.Process.setThreadPriority(THREAD_PRIORITY_LOWEST);
                        r.run();
                      }
                    },
                    "Retrofit-Idle-Background");
              }
            });
    restAdapter =
        new RestAdapter.Builder()
            .setLogLevel(RestAdapter.LogLevel.BASIC)
            .setEndpoint(HATCHET_BASE_URL + HATCHET_API_VERSION)
            .setConverter(new GsonConverter(GsonHelper.get()))
            .setRequestInterceptor(requestInterceptor)
            .setClient(new OkClient(mOkHttpClient))
            .setExecutors(httpExecutor, new MainThreadExecutor())
            .build();
    mHatchetBackground = restAdapter.create(Hatchet.class);

    mCache.put(TYPE_IMAGES, new ConcurrentHashMap<String, Image>());
    mCache.put(TYPE_ARTISTS, new ConcurrentHashMap<String, Artist>());
    mCache.put(TYPE_ALBUMS, new ConcurrentHashMap<String, Album>());
    mCache.put(TYPE_TRACKS, new ConcurrentHashMap<String, Query>());
    mCache.put(TYPE_USERS, new ConcurrentHashMap<String, User>());
    mCache.put(TYPE_PLAYLISTENTRIES, new ConcurrentHashMap<String, Query>());
    mCache.put(TYPE_PLAYLISTS, new ConcurrentHashMap<String, Playlist>());
    mCache.put(TYPE_SOCIALACTIONS, new ConcurrentHashMap<String, SocialAction>());
    mCache.put(TYPE_SEARCHES, new ConcurrentHashMap<String, Search>());
    mCache.put(TYPE_SEARCHRESULTS, new ConcurrentHashMap<String, SearchResult>());
  }