Ejemplo n.º 1
0
 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (resultCode == Activity.RESULT_OK && requestCode == STORY_REQUEST_CODE) {
     LogUtil.w("Latest---onActivityResult");
     // 如果是已读的话,更新整个数据源
     if (mClickItemPosition != -1) {
       View view =
           mStoryList.getChildAt(mClickItemPosition - mStoryList.getFirstVisiblePosition());
       TextView txtView = (TextView) view.findViewById(R.id.title_story);
       txtView.setTextColor(Color.GRAY);
     }
   }
 }
Ejemplo n.º 2
0
  @Override
  @SuppressWarnings("unchecked")
  protected ResultType doBackground() throws Throwable {

    if (this.isCancelled()) {
      throw new Callback.CancelledException("cancelled before request");
    }

    // 初始化请求参数
    ResultType result = null;
    boolean retry = true;
    int retryCount = 0;
    Throwable exception = null;
    HttpRetryHandler retryHandler = new HttpRetryHandler(this.params.getMaxRetryCount());
    request = initRequest();

    if (this.isCancelled()) {
      throw new Callback.CancelledException("cancelled before request");
    }

    // 检查缓存
    Object cacheResult = null;
    if (cacheCallback != null && HttpMethod.permitsCache(params.getMethod())) {
      // 尝试从缓存获取结果, 并为请求头加入缓存控制参数.
      while (retry) {
        try {
          clearRawResult();
          rawResult = this.request.loadResultFromCache();
          break;
        } catch (Throwable ex) {
          LogUtil.w("load disk cache error", ex);
          exception = ex;
          retry = retryHandler.retryRequest(ex, ++retryCount, this.request);
        }
      }

      if (this.isCancelled()) {
        clearRawResult();
        throw new Callback.CancelledException("cancelled before request");
      }

      if (rawResult != null) {
        if (prepareCallback != null) {
          try {
            cacheResult = prepareCallback.prepare(rawResult);
          } catch (Throwable ex) {
            cacheResult = null;
            LogUtil.w("prepare disk cache error", ex);
          } finally {
            clearRawResult();
          }
        } else {
          cacheResult = rawResult;
        }

        if (this.isCancelled()) {
          throw new Callback.CancelledException("cancelled before request");
        }

        if (cacheResult != null) {
          // 同步等待是否信任缓存
          this.update(FLAG_CACHE, cacheResult);
          while (trustCache == null) {
            synchronized (cacheLock) {
              try {
                cacheLock.wait();
              } catch (Throwable ignored) {
              }
            }
          }

          // 处理完成
          if (trustCache) {
            return null;
          }
        }
      }
    }

    if (trustCache == null) {
      trustCache = false;
    }

    if (cacheResult == null) {
      this.request.clearCacheHeader();
    }

    // 发起请求
    retry = true;
    while (retry) {

      try {
        if (this.isCancelled()) {
          throw new Callback.CancelledException("cancelled before request");
        }

        // 由loader发起请求, 拿到结果.
        this.request.close(); // retry 前关闭上次请求

        try {
          clearRawResult();
          requestWorker = new RequestWorker(this.request, this.loadType);
          if (params.isCancelFast()) {
            requestWorker.start();
            requestWorker.join();
          } else {
            requestWorker.run();
          }
          if (requestWorker.ex != null) {
            throw requestWorker.ex;
          }
          rawResult = requestWorker.result;
        } catch (Throwable ex) {
          clearRawResult();
          if (this.isCancelled()) {
            throw new Callback.CancelledException("cancelled during request");
          } else {
            throw ex;
          }
        }

        if (prepareCallback != null) {
          try {
            result = (ResultType) prepareCallback.prepare(rawResult);
          } finally {
            clearRawResult();
          }
        } else {
          result = (ResultType) rawResult;
        }

        // 保存缓存
        if (cacheCallback != null && HttpMethod.permitsCache(params.getMethod())) {
          this.request.save2Cache();
        }

        retry = false;

        if (this.isCancelled()) {
          throw new Callback.CancelledException("cancelled after request");
        }
      } catch (Throwable ex) {
        if (this.request.getResponseCode() == 304) { // disk cache is valid.
          return null;
        } else {
          exception = ex;
          retry = retryHandler.retryRequest(ex, ++retryCount, this.request);
        }
      }
    }

    if (exception != null && result == null && !trustCache) {
      throw exception;
    }

    return result;
  }