Exemplo n.º 1
0
 @Override
 public Response execute() throws IOException {
   synchronized (this) {
     if (executed) throw new IllegalStateException("Already Executed");
     executed = true;
   }
   try {
     client.dispatcher().executed(this);
     Response result = getResponseWithInterceptorChain();
     if (result == null) throw new IOException("Canceled");
     return result;
   } finally {
     client.dispatcher().finished(this);
   }
 }
  public FootballDataAPI(Context context) {
    mContext = context;

    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    // set your desired log level
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);

    mHttpClient =
        new OkHttpClient.Builder()
            .addInterceptor(new HeaderInterceptor())
            .addInterceptor(new ApiLimitInterceptor())
            .addInterceptor(logging)
            .build();

    // limit the number of concurrent async calls
    mHttpClient.dispatcher().setMaxRequests(5);

    Retrofit retrofit =
        new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(mHttpClient)
            .build();

    mApiService = retrofit.create(FootballDataService.class);
  }
Exemplo n.º 3
0
 @Override
 public void enqueue(Callback responseCallback) {
   synchronized (this) {
     if (executed) throw new IllegalStateException("Already Executed");
     executed = true;
   }
   client.dispatcher().enqueue(new AsyncCall(responseCallback));
 }
Exemplo n.º 4
0
 public void cancelTag(Object tag) {
   Dispatcher dispatcher = mClient.dispatcher();
   List<Call> calls = dispatcher.queuedCalls();
   for (Call call : calls) {
     if (call.request().tag().equals(tag)) {
       call.cancel();
     }
   }
   calls = dispatcher.runningCalls();
   for (Call call : calls) {
     if (call.request().tag().equals(tag)) {
       call.cancel();
     }
   }
 }
Exemplo n.º 5
0
 /**
  * 取消相同的tag请求
  *
  * @param tag
  */
 public static void cancelSameTagCall(Object tag) {
   if (mClient != null) {
     Dispatcher dispatcher = mClient.dispatcher();
     for (Call call : dispatcher.queuedCalls()) {
       if (tag.equals(call.request().tag())) {
         call.cancel();
       }
     }
     for (Call call : dispatcher.runningCalls()) {
       if (tag.equals(call.request().tag())) {
         call.cancel();
       }
     }
     setCancel(true);
   } else {
     Logger.e(TAG, "cancelSameTagCall mClient 为 null");
   }
 }
Exemplo n.º 6
0
 public void cancelAll() {
   mClient.dispatcher().cancelAll();
 }