• <i id='cXp1r'><tr id='cXp1r'><dt id='cXp1r'><q id='cXp1r'><span id='cXp1r'><b id='cXp1r'><form id='cXp1r'><ins id='cXp1r'></ins><ul id='cXp1r'></ul><sub id='cXp1r'></sub></form><legend id='cXp1r'></legend><bdo id='cXp1r'><pre id='cXp1r'><center id='cXp1r'></center></pre></bdo></b><th id='cXp1r'></th></span></q></dt></tr></i><div id='cXp1r'><tfoot id='cXp1r'></tfoot><dl id='cXp1r'><fieldset id='cXp1r'></fieldset></dl></div>

    <small id='cXp1r'></small><noframes id='cXp1r'>

      • <bdo id='cXp1r'></bdo><ul id='cXp1r'></ul>
    1. <legend id='cXp1r'><style id='cXp1r'><dir id='cXp1r'><q id='cXp1r'></q></dir></style></legend>

        <tfoot id='cXp1r'></tfoot>

      1. 如何向 OkHttp 请求拦截器添加标头?

        How to add headers to OkHttp request interceptor?(如何向 OkHttp 请求拦截器添加标头?)
        <tfoot id='Yice2'></tfoot>

          • <small id='Yice2'></small><noframes id='Yice2'>

            <legend id='Yice2'><style id='Yice2'><dir id='Yice2'><q id='Yice2'></q></dir></style></legend>

            <i id='Yice2'><tr id='Yice2'><dt id='Yice2'><q id='Yice2'><span id='Yice2'><b id='Yice2'><form id='Yice2'><ins id='Yice2'></ins><ul id='Yice2'></ul><sub id='Yice2'></sub></form><legend id='Yice2'></legend><bdo id='Yice2'><pre id='Yice2'><center id='Yice2'></center></pre></bdo></b><th id='Yice2'></th></span></q></dt></tr></i><div id='Yice2'><tfoot id='Yice2'></tfoot><dl id='Yice2'><fieldset id='Yice2'></fieldset></dl></div>

              <tbody id='Yice2'></tbody>
              • <bdo id='Yice2'></bdo><ul id='Yice2'></ul>
                1. 本文介绍了如何向 OkHttp 请求拦截器添加标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我将这个拦截器添加到我的 OkHttp 客户端:

                  I have this interceptor that i add to my OkHttp client:

                  public class RequestTokenInterceptor implements Interceptor {
                  @Override
                  public Response intercept(Chain chain) throws IOException {
                    Request request = chain.request();
                    // Here where we'll try to refresh token.
                    // with an retrofit call
                    // After we succeed we'll proceed our request
                    Response response = chain.proceed(request);
                    return response;
                  }
                  }
                  

                  如何在拦截器中添加请求头?

                  How can i add headers to request in my interceptor?

                  我试过了,但我犯了错误,我在创建新请求时丢失了我的请求:

                  I tried this but i am making mistake and i lose my request when creating new request:

                      public class RequestTokenInterceptor implements Interceptor {
                      @Override
                      public Response intercept(Interceptor.Chain chain) throws IOException {
                          Request request = chain.request();
                          Request newRequest;
                  
                          try {
                              Log.d("addHeader", "Before");
                              String token = TokenProvider.getInstance(mContext).getToken();
                              newRequest = request.newBuilder()
                                      .addHeader(HeadersContract.HEADER_AUTHONRIZATION, O_AUTH_AUTHENTICATION + token)
                                      .addHeader(HeadersContract.HEADER_CLIENT_ID, CLIENT_ID)
                                      .build();
                          } catch (Exception e) {
                              Log.d("addHeader", "Error");
                              e.printStackTrace();
                              return chain.proceed(request);
                          }
                  
                          Log.d("addHeader", "after");
                          return chain.proceed(newRequest);
                      }
                  }
                  

                  请注意,我知道我可以在创建这样的请求时添加标头:

                  Note that, i know i can add header when creating request like this:

                  Request request = new Request.Builder()
                      .url("https://api.github.com/repos/square/okhttp/issues")
                      .header("User-Agent", "OkHttp Headers.java")
                      .addHeader("Accept", "application/json; q=0.5")
                      .addHeader("Accept", "application/vnd.github.v3+json")
                      .build();
                  

                  但这不符合我的需要.我需要它在拦截器中.

                  But it doesn't fit my needs. I need it in interceptor.

                  推荐答案

                  最后,我是这样添加标题的:

                  Finally, I added the headers this way:

                  @Override
                      public Response intercept(Interceptor.Chain chain) throws IOException {
                          Request request = chain.request();
                          Request newRequest;
                  
                          newRequest = request.newBuilder()
                                  .addHeader(HeadersContract.HEADER_AUTHONRIZATION, O_AUTH_AUTHENTICATION)
                                  .addHeader(HeadersContract.HEADER_X_CLIENT_ID, CLIENT_ID)
                                  .build();
                          return chain.proceed(newRequest);
                      }
                  

                  这篇关于如何向 OkHttp 请求拦截器添加标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                    <tfoot id='zIqwi'></tfoot>
                    • <bdo id='zIqwi'></bdo><ul id='zIqwi'></ul>

                      • <small id='zIqwi'></small><noframes id='zIqwi'>

                            <tbody id='zIqwi'></tbody>
                        1. <legend id='zIqwi'><style id='zIqwi'><dir id='zIqwi'><q id='zIqwi'></q></dir></style></legend>
                            <i id='zIqwi'><tr id='zIqwi'><dt id='zIqwi'><q id='zIqwi'><span id='zIqwi'><b id='zIqwi'><form id='zIqwi'><ins id='zIqwi'></ins><ul id='zIqwi'></ul><sub id='zIqwi'></sub></form><legend id='zIqwi'></legend><bdo id='zIqwi'><pre id='zIqwi'><center id='zIqwi'></center></pre></bdo></b><th id='zIqwi'></th></span></q></dt></tr></i><div id='zIqwi'><tfoot id='zIqwi'></tfoot><dl id='zIqwi'><fieldset id='zIqwi'></fieldset></dl></div>