okhttp3.Call.request()方法的使用及代码示例

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

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

Call.request介绍

[英]Returns the original request that initiated this call.
[中]返回发起此调用的原始请求。

代码示例

代码示例来源:origin: square/okhttp

  1. @Override
  2. public void callStart(Call call) {
  3. startNs = System.nanoTime();
  4. logWithTime("callStart: " + call.request());
  5. }

代码示例来源:origin: square/okhttp

  1. @Override public EventListener create(Call call) {
  2. long callId = nextCallId.getAndIncrement();
  3. System.out.printf("%04d %s%n", callId, call.request().url());
  4. return new PrintingEventListener(callId, System.nanoTime());
  5. }
  6. };

代码示例来源:origin: square/retrofit

  1. @Override public synchronized Request request() {
  2. okhttp3.Call call = rawCall;
  3. if (call != null) {
  4. return call.request();
  5. }
  6. if (creationFailure != null) {
  7. if (creationFailure instanceof IOException) {
  8. throw new RuntimeException("Unable to create request.", creationFailure);
  9. } else if (creationFailure instanceof RuntimeException) {
  10. throw (RuntimeException) creationFailure;
  11. } else {
  12. throw (Error) creationFailure;
  13. }
  14. }
  15. try {
  16. return (rawCall = createRawCall()).request();
  17. } catch (RuntimeException | Error e) {
  18. throwIfFatal(e); // Do not assign a fatal error to creationFailure.
  19. creationFailure = e;
  20. throw e;
  21. } catch (IOException e) {
  22. creationFailure = e;
  23. throw new RuntimeException("Unable to create request.", e);
  24. }
  25. }

代码示例来源:origin: com.squareup.okhttp3/logging-interceptor

  1. @Override
  2. public void callStart(Call call) {
  3. startNs = System.nanoTime();
  4. logWithTime("callStart: " + call.request());
  5. }

代码示例来源:origin: com.squareup.retrofit2/retrofit

  1. @Override public synchronized Request request() {
  2. okhttp3.Call call = rawCall;
  3. if (call != null) {
  4. return call.request();
  5. }
  6. if (creationFailure != null) {
  7. if (creationFailure instanceof IOException) {
  8. throw new RuntimeException("Unable to create request.", creationFailure);
  9. } else if (creationFailure instanceof RuntimeException) {
  10. throw (RuntimeException) creationFailure;
  11. } else {
  12. throw (Error) creationFailure;
  13. }
  14. }
  15. try {
  16. return (rawCall = createRawCall()).request();
  17. } catch (RuntimeException | Error e) {
  18. throwIfFatal(e); // Do not assign a fatal error to creationFailure.
  19. creationFailure = e;
  20. throw e;
  21. } catch (IOException e) {
  22. creationFailure = e;
  23. throw new RuntimeException("Unable to create request.", e);
  24. }
  25. }

代码示例来源:origin: square/okhttp

  1. networkInterceptor.proceed();
  2. OutputStreamRequestBody requestBody = (OutputStreamRequestBody) call.request().body();
  3. if (requestBody != null) requestBody.outputStream().close();

代码示例来源:origin: jeasonlzy/okhttp-OkGo

  1. /** 根据Tag取消请求 */
  2. public static void cancelTag(OkHttpClient client, Object tag) {
  3. if (client == null || tag == null) return;
  4. for (Call call : client.dispatcher().queuedCalls()) {
  5. if (tag.equals(call.request().tag())) {
  6. call.cancel();
  7. }
  8. }
  9. for (Call call : client.dispatcher().runningCalls()) {
  10. if (tag.equals(call.request().tag())) {
  11. call.cancel();
  12. }
  13. }
  14. }

代码示例来源:origin: jeasonlzy/okhttp-OkGo

  1. /** 根据Tag取消请求 */
  2. public void cancelTag(Object tag) {
  3. if (tag == null) return;
  4. for (Call call : getOkHttpClient().dispatcher().queuedCalls()) {
  5. if (tag.equals(call.request().tag())) {
  6. call.cancel();
  7. }
  8. }
  9. for (Call call : getOkHttpClient().dispatcher().runningCalls()) {
  10. if (tag.equals(call.request().tag())) {
  11. call.cancel();
  12. }
  13. }
  14. }

代码示例来源:origin: square/okhttp

  1. @Override public OutputStream getOutputStream() throws IOException {
  2. OutputStreamRequestBody requestBody = (OutputStreamRequestBody) buildCall().request().body();
  3. if (requestBody == null) {
  4. throw new ProtocolException("method does not support a request body: " + method);
  5. }
  6. // If this request needs to stream bytes to the server, build a physical connection immediately
  7. // and start streaming those bytes over that connection.
  8. if (requestBody instanceof StreamedRequestBody) {
  9. connect();
  10. networkInterceptor.proceed();
  11. }
  12. if (requestBody.isClosed()) {
  13. throw new ProtocolException("cannot write request body after response has been read");
  14. }
  15. return requestBody.outputStream();
  16. }

代码示例来源:origin: tianshaojie/AndroidFine

  1. @Override
  2. public void onFailure(Call call, IOException e) {
  3. handler.sendFailureMessage(call.request(), e);
  4. }
  5. });

代码示例来源:origin: tianshaojie/AndroidFine

  1. @Override
  2. public void onFailure(Call call, IOException e) {
  3. handler.sendFailureMessage(call.request(), e);
  4. }
  5. });

代码示例来源:origin: tianshaojie/AndroidFine

  1. @Override
  2. public void onResponse(Call call, Response response) {
  3. try {
  4. RestApiResponse apiResponse = getRestApiResponse(response.body().toString());
  5. handler.sendSuccessMessage(apiResponse);
  6. } catch (Exception e) {
  7. handler.sendFailureMessage(call.request(), e);
  8. }
  9. }

代码示例来源:origin: tianshaojie/AndroidFine

  1. @Override
  2. public void onResponse(Call call, Response response) {
  3. try {
  4. RestApiResponse apiResponse = getRestApiResponse(response.body().toString());
  5. handler.sendSuccessMessage(apiResponse);
  6. } catch (Exception e) {
  7. handler.sendFailureMessage(call.request(), e);
  8. }
  9. }

代码示例来源:origin: dongjunkun/GanK

  1. /**
  2. * 根据tag取消请求
  3. *
  4. * @param tag 标签
  5. */
  6. public static void cancelRequest(Object tag) {
  7. for (Call call : okHttpClient.dispatcher().queuedCalls()) {
  8. if (call.request().tag().equals(tag)) {
  9. call.cancel();
  10. }
  11. }
  12. for (Call call : okHttpClient.dispatcher().runningCalls()) {
  13. if (call.request().tag().equals(tag)) {
  14. call.cancel();
  15. }
  16. }
  17. }

代码示例来源:origin: limedroid/XDroid

  1. @Override
  2. public void onError(Call call, Exception e, int id) {
  3. if (e != null) {
  4. if (e instanceof UnknownHostException
  5. && expireTime > -1) {
  6. //无网络连接
  7. String cacheKey = getCacheKey(call.request());
  8. String cacheContent = DiskCache.getInstance(App.getContext()).get(cacheKey);
  9. if (!TextUtils.isEmpty(cacheContent)) {
  10. try {
  11. if (entityClass == String.class) {
  12. onResponse((T) cacheContent, id);
  13. return;
  14. }
  15. T model = GsonProvider.getInstance().getGson().fromJson(cacheContent, entityClass);
  16. onResponse(model, id);
  17. return;
  18. } catch (Exception exception) {
  19. }
  20. }
  21. }
  22. }
  23. onFail(call, e, id);
  24. }

代码示例来源:origin: stackoverflow.com

  1. @Override
  2. public void onResponse(Call call, final Response response) throws IOException {
  3. if(response.isSuccessful()) {
  4. String responseString = response.body().string();
  5. postSuccess(listener, (String)call.request().tag(), responseString);
  6. }
  7. else {
  8. String errorBodyString = response.body().string();
  9. postFailure(listener, (String)call.request().tag(),response.code()+","+errorBodyString);
  10. }
  11. }

代码示例来源:origin: ApolloAuto/apollo-DuerOS

  1. @Override
  2. public void onFailure(Call call, final IOException e) {
  3. errorCallback(callback, call.request().url().toString(), e.toString());
  4. }
  5. });

代码示例来源:origin: watson-developer-cloud/java-sdk

  1. @Override
  2. public ServiceCall<T> addHeader(String name, String value) {
  3. Request.Builder builder = call.request().newBuilder();
  4. builder.header(name, value);
  5. call = client.newCall(builder.build());
  6. return this;
  7. }

代码示例来源:origin: ApolloAuto/apollo-DuerOS

  1. @Override
  2. public void onFailure(Call call, final IOException e) {
  3. e.printStackTrace();
  4. downloadErrorCallback(downloadCallback, call.request().url().toString(), e.toString());
  5. }

代码示例来源:origin: com.ibm.watson.developer_cloud/core

  1. @Override
  2. protected void finalize() throws Throwable {
  3. super.finalize();
  4. if (!call.isExecuted()) {
  5. final Request r = call.request();
  6. LOG.warning(r.method() + " request to " + r.url() + " has not been sent. Did you forget to call execute()?");
  7. }
  8. }
  9. }

相关文章