com.squareup.okhttp.Call.enqueue()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(301)

本文整理了Java中com.squareup.okhttp.Call.enqueue()方法的一些代码示例,展示了Call.enqueue()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Call.enqueue()方法的具体详情如下:
包路径:com.squareup.okhttp.Call
类名称:Call
方法名:enqueue

Call.enqueue介绍

暂无

代码示例

代码示例来源:origin: xinghongfei/LookLook

private void deliveryResult(final ResultCallback callback, Request request) {
  mOkHttpClient.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Request request, final IOException e) {
      sendFailCallback(callback, e);
    }
    @Override
    public void onResponse(Response response) throws IOException {
      try {
        String str = response.body().string();
        if (callback.mType == String.class) {
          sendSuccessCallBack(callback, str);
        } else {
          Object object = JsonUtils.deserialize(str, callback.mType);
          sendSuccessCallBack(callback, object);
        }
      } catch (final Exception e) {
        sendFailCallback(callback, e);
      }
    }
  });
}

代码示例来源:origin: bumptech/glide

@Override
public void loadData(@NonNull Priority priority,
  @NonNull final DataCallback<? super InputStream> callback) {
 Request.Builder requestBuilder = new Request.Builder().url(url.toStringUrl());
 for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
  String key = headerEntry.getKey();
  requestBuilder.addHeader(key, headerEntry.getValue());
 }
 Request request = requestBuilder.build();
 client.newCall(request).enqueue(new com.squareup.okhttp.Callback() {
  @Override
  public void onFailure(Request request, IOException e) {
   if (Log.isLoggable(TAG, Log.DEBUG)) {
    Log.d(TAG, "OkHttp failed to obtain result", e);
   }
   callback.onLoadFailed(e);
  }
  @Override
  public void onResponse(Response response) throws IOException {
   responseBody = response.body();
   if (response.isSuccessful()) {
    long contentLength = responseBody.contentLength();
    stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
    callback.onDataReady(stream);
   } else {
    callback.onLoadFailed(new HttpException(response.message(), response.code()));
   }
  }
 });
}

代码示例来源:origin: commonsguy/cw-omnibus

@Override
public void onViewCreated(View view,
             Bundle savedInstanceState) {
 super.onViewCreated(view, savedInstanceState);
 Request request=new Request.Builder()
  .url(URL_BACKUPS)
  .build();
 BackupService.OKHTTP_CLIENT.newCall(request).enqueue(this);
}

代码示例来源:origin: spotify/apollo

finalClient.newCall(request).enqueue(TransformingCallback.create(result));

代码示例来源:origin: jiangqqlmj/FastDev4Android

.url("http://www.baidu.com")
    .build();
client.newCall(request).enqueue(new Callback() {
  @Override
  public void onFailure(Request request, IOException e) {

代码示例来源:origin: cn-ljb/rxjava_for_android

/**
 * 异步CallBack模式
 *
 * @param request
 * @param responseCallback
 */
public void enqueue(Request request, Callback responseCallback) {
  mOkHttpClient.newCall(request).enqueue(responseCallback);
}

代码示例来源:origin: liuling07/SimpleNews

private void deliveryResult(final ResultCallback callback, Request request) {
  mOkHttpClient.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Request request, final IOException e) {
      sendFailCallback(callback, e);
    }
    @Override
    public void onResponse(Response response) throws IOException {
      try {
        String str = response.body().string();
        if (callback.mType == String.class) {
          sendSuccessCallBack(callback, str);
        } else {
          Object object = JsonUtils.deserialize(str, callback.mType);
          sendSuccessCallBack(callback, object);
        }
      } catch (final Exception e) {
        LogUtils.e(TAG, "convert json failure", e);
        sendFailCallback(callback, e);
      }
    }
  });
}

代码示例来源:origin: survivingwithandroid/Surviving-with-android

.build();
client.newCall(req).enqueue(new Callback() {
  @Override
  public void onFailure(Request request, IOException e) {

代码示例来源:origin: apache/servicemix-bundles

public OkHttpListenableFuture(Call call) {
  this.call = call;
  this.call.enqueue(new Callback() {
    @Override
    public void onResponse(Response response) {
      set(new OkHttpClientHttpResponse(response));
    }
    @Override
    public void onFailure(Request request, IOException ex) {
      setException(ex);
    }
  });
}

代码示例来源:origin: liferay/liferay-mobile-sdk

protected void sendAsync(Call call, final Callback callback) {
  call.enqueue(new com.squareup.okhttp.Callback() {
    @Override
    public void onFailure(
      com.squareup.okhttp.Request request, IOException ioe) {
      callback.doFailure(ioe);
    }
    @Override
    public void onResponse(com.squareup.okhttp.Response response)
      throws IOException {
      callback.inBackground(new Response(response));
    }
  });
}

代码示例来源:origin: fire3/sailorcast

public static void asyncGet(Request request, Callback callback) {
  Log.d("fire3", "async request Url: " + request.urlString());
  SailorCast.getHttpClient().newCall(request).enqueue(callback);
}

代码示例来源:origin: fstab/http2-examples

public static void main(String[] args) throws Exception {
  OkHttpClient client = getUnsafeOkHttpClient();
  Request request = new Request.Builder()
      .url("https://localhost:8443") // The Http2Server should be running here.
      .build();
  long startTime = System.nanoTime();
  for (int i=0; i<3; i++) {
    Thread.sleep(1000); // http://stackoverflow.com/questions/32625035/when-using-http2-in-okhttp-why-multi-requests-to-the-same-host-didnt-use-just
    client.newCall(request).enqueue(new Callback() {
      public void onFailure(Request request, IOException e) {
        e.printStackTrace();
      }
      public void onResponse(Response response) throws IOException {
        long duration = TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startTime);
        System.out.println("After " + duration + " seconds: " + response.body().string());
      }
    });
  }
}

代码示例来源:origin: florent37/TutosAndroidFrance

public void get(){
  //get Request
  Request myGetRequest = new Request.Builder()
      .url("https://api.github.com/users/florent37")
      .build();
  okHttpClient.newCall(myGetRequest).enqueue(new Callback() {
    @Override
    public void onFailure(Request request, IOException e) {
    }
    @Override
    public void onResponse(Response response) throws IOException {
      //le retour est effectué dans un thread différent
      final String text = response.body().string();
      final int statusCode = response.code();
      runOnUiThread(new Runnable() {
        @Override
        public void run() {
          textView.setText(text);
        }
      });
    }
  });
}

代码示例来源:origin: cyforever123/ElectronicbrandProject

/**
 * 异步Get请求 具体实现
 *
 * @param url             请求url
 * @param iTRequestResult 请求回调
 * @param clazz           Class<T>
 * @param params          请求参数
 * @param <T>             泛型模板
 */
public <T> void requestAsyncGetEnqueue(String url, ITRequestResult<T> iTRequestResult, Class<T> clazz, Param... params) {
  String constructUrl = constructUrl(url, params);
  Request request = new Request.Builder()
      .get()
      .url(constructUrl)
      .build();
  mOkHttpClient.newCall(request).enqueue(new TRequestCallBack(iTRequestResult, clazz));
}

代码示例来源:origin: cyforever123/ElectronicbrandProject

/**
 * 异步DELETE请求  具体实现
 *
 * @param url             请求url
 * @param iTRequestResult 请求回调
 * @param clazz           Class<T>
 * @param params          请求参数
 * @param <T>             泛型模板
 */
public <T> void requestAsyncDelete(String url, ITRequestResult<T> iTRequestResult, Class<T> clazz, Param... params) {
  String finalUrl = constructUrl(url, params);
  Request request = new Request.Builder()
      .delete()
      .url(finalUrl)
      .build();
  mOkHttpClient.newCall(request).enqueue(new TRequestCallBack(iTRequestResult, clazz));
}
/*********************************************************** 文件请求*********************************************************/

代码示例来源:origin: liferay/liferay-mobile-sdk

public static void signIn(
    Session session, CookieCallback callback,
    Authenticator authenticator) {
  try {
    CookieSignIn cookieSignIn = new CookieSignIn(
        session);
    Call call = cookieSignIn.signIn();
    Callback requestCallback = getCallback(
      session.getServer(), callback, cookieSignIn.cookieManager,
      getCookieAuthentication(session.getAuthentication()));
    call.enqueue(requestCallback);
  }
  catch (Exception e) {
    callback.onFailure(e);
  }
}

代码示例来源:origin: com.dropbox.core/dropbox-core-sdk

@Override
public OutputStream getBody() {
  // getBody() can be called multiple times to get access to the output stream. Don't
  // error if this is the case.
  if (body instanceof PipedRequestBody) {
    return ((PipedRequestBody) body).getOutputStream();
  } else {
    PipedRequestBody pipedBody = new PipedRequestBody();
    if (progressListener != null) {
      pipedBody.setListener(progressListener);
    }
    setBody(pipedBody);
    this.callback = new AsyncCallback();
    this.call = client.newCall(request.build());
    // enqueue the call (async call execution). This allows us to provide streaming uploads.
    call.enqueue(callback);
    return pipedBody.getOutputStream();
  }
}

代码示例来源:origin: cyforever123/ElectronicbrandProject

/**
 * 异步Get请求 具体实现(可取消)
 *
 * @param url             请求url
 * @param activityName    请求activityName
 * @param iTRequestResult 请求回调
 * @param clazz           Class<T>
 * @param params          请求参数
 * @param <T>             泛型模板
 */
public <T> void requestAsyncGetEnqueueByTag(String url, String activityName, ITRequestResult<T> iTRequestResult, Class<T> clazz, Param... params) {
  addRequestUrl(activityName, url);
  String constructUrl = constructUrl(url, params);
  Request request = new Request.Builder()
      .get()
      .url(constructUrl)
      .tag(url)
      .build();
  mOkHttpClient.newCall(request).enqueue(new TRequestCallBack(iTRequestResult, clazz, activityName));
}

代码示例来源:origin: cyforever123/ElectronicbrandProject

/**
 * 异步POST请求  具体实现
 *
 * @param url             请求url
 * @param iTRequestResult 请求回调
 * @param clazz           Class<T>
 * @param params          请求参数
 * @param <T>             泛型模板
 */
public <T> void requestAsyncPost(String url, ITRequestResult<T> iTRequestResult, Class<T> clazz, Param... params) {
  FormEncodingBuilder builder = new FormEncodingBuilder();
  for (Param param :
      params) {
    builder.add(param.key, param.value);
  }
  RequestBody body = builder.build();
  Request request = new Request.Builder().post(body).url(url).build();
  mOkHttpClient.newCall(request).enqueue(new TRequestCallBack(iTRequestResult, clazz));
}

代码示例来源:origin: cyforever123/ElectronicbrandProject

/**
 * 异步POST请求  具体实现(可取消)
 *
 * @param url             请求url
 * @param activityName    请求activityName
 * @param iTRequestResult 请求回调
 * @param clazz           Class<T>
 * @param params          请求参数
 * @param <T>             泛型模板
 */
public <T> void requestAsyncPostByTag(String url, String activityName, ITRequestResult<T> iTRequestResult, Class<T> clazz, Param... params) {
  addRequestUrl(activityName, url);
  FormEncodingBuilder builder = new FormEncodingBuilder();
  for (Param param :
      params) {
    builder.add(param.key, param.value);
  }
  RequestBody body = builder.build();
  Request request = new Request.Builder().post(body).url(url).tag(url).build();
  mOkHttpClient.newCall(request).enqueue(new TRequestCallBack(iTRequestResult, clazz, activityName));
}

相关文章