- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我在 Android 应用程序中使用带有 OkHttp 客户端的 Retrofit 2 (2.0.0-beta3),到目前为止一切顺利。但目前我正面临 OkHttp 拦截器的问题。我正在与之通信的服务器正在请求正文中获取访问 token ,因此当我拦截添加身份验证 token 的请求或在 Authenticator 的身份验证方法中,当我需要添加更新的身份验证 token 时,我需要为此修改请求正文.但看起来我只能在 header 中添加数据,而不能在正在进行的请求的正文中添加数据。目前我写的代码如下:
client.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (UserPreferences.ACCESS_TOKEN != null) {
// need to add this access token in request body as encoded form field instead of header
request = request.newBuilder()
.header("access_token", UserPreferences.ACCESS_TOKEN))
.method(request.method(), request.body())
.build();
}
Response response = chain.proceed(request);
return response;
}
});
谁能指出我正确的方向,如如何修改请求正文以添加我的访问 token (第一次或在 token 刷新后更新)?任何指向正确方向的指针都将不胜感激。
最佳答案
我使用它来将 post 参数添加到现有的。
OkHttpClient client = new OkHttpClient.Builder()
.protocols(protocols)
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request.Builder requestBuilder = request.newBuilder();
RequestBody formBody = new FormEncodingBuilder()
.add("email", "Jurassic@Park.com")
.add("tel", "90301171XX")
.build();
String postBodyString = Utils.bodyToString(request.body());
postBodyString += ((postBodyString.length() > 0) ? "&" : "") + Utils.bodyToString(formBody);
request = requestBuilder
.post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded;charset=UTF-8"), postBodyString))
.build();
return chain.proceed(request);
}
})
.build();
public static String bodyToString(final RequestBody request){
try {
final RequestBody copy = request;
final Buffer buffer = new Buffer();
if(copy != null)
copy.writeTo(buffer);
else
return "";
return buffer.readUtf8();
}
catch (final IOException e) {
return "did not work";
}
}
OkHttp3:
RequestBody formBody = new FormBody.Builder()
.add("email", "Jurassic@Park.com")
.add("tel", "90301171XX")
.build();
关于android - 改造2 : Modifying request body in OkHttp Interceptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34791244/
我正在实现 Server-Sent Events使用 OkHttp 的库。服务器发送事件的工作原理是与服务器保持开放的 HTTP 连接,在该服务器上,“事件”可以流式传输回客户端。连接只会在错误或客户
我需要用 OkHttp 重现错误,这样我就可以提交错误或在 StackOverflow 上提问。 无需大量设置即可执行此操作的最简单方法是什么? 我读过 https://stackoverflow.c
我需要用 OkHttp 重现错误,这样我就可以提交错误或在 StackOverflow 上提问。 无需大量设置即可执行此操作的最简单方法是什么? 我读过 https://stackoverflow.c
我正在尝试使用 OkHttp 与 HTTP/2 服务器进行通信客户端。 添加到 Maven POM: com.squareup.okhttp3 okhttp 3.2.0 这是我的
我在使用我的应用程序时不断在 Logcat 中看到以下日志行: 19098-19147/ W/OkHttpClient: A connection to was leaked. Did you fo
我的团队正受此困扰issue使用 slack 集成来上传文件,因此根据该问题中的评论,我想限制我们的 Kotlin 实现中的请求。 我正在尝试集成 Okio Throttler在 OkHttp 拦截器
如何验证 okhttp 是否成功协商了 http/2 而没有使用 https/1.1?我可以检查回复中的一些信息吗? 谢谢, -托尼 最佳答案 仅供引用,因为 okhttp 3.0 左右 OkHttp
我想提出一个请求并强制它使用 Protocol.HTTP_2。 我尝试了下面的代码: import okhttp3.{OkHttpClient, Protocol, Request} import s
我不太熟悉 OkHttp 库,我对它应该如何用于异步请求有一些疑问。 对于 Android 编程,我将 android 库用于 http,并将 asyncTask 用于某些请求。 但是因为我想尝试 O
我在我的项目中使用 Retrofit-1.6.0 + okhttp-2.0.0-RC1 。当我运行项目时出现错误。 java.lang.RuntimeException: Retrofit detec
OkHttpClient 的默认行为是将 keep-alive 设置为 true。对于我们的特定用例,我需要将其设置为 false。 我没有在 Java 源代码中找到任何设置来执行此操作。 有人遇到过
我试图让 OkHttp 在我的 Android Studio 项目中工作,我创建了一个类并从这里粘贴以下代码:https://github.com/square/okhttp/blob/master/
我注意到 OkHttp 在 Android 5.0+ 中通过 ALPN 支持 h2。 来自 some article ,我了解到我们可以对 http/2 请求使用 NPN 协商。 我可以通过 NPN
我知道,Retrofit 在内部使用 OkHttp。但是,我可以看到一些开发人员提供了以下方法的应用 return new Retrofit.Builder() .baseUrl(Bu
我正在努力让 Android 上的 OkHttpClient 使用自定义证书发出 HTTPS 请求,同时绑定(bind)到特定网络接口(interface)的本地地址。我目前对此的尝试使用以下 OkH
我有一个用于 https 连接演示的 native android 应用程序,我遇到了这样的异常:: 我的连接代码很简单: URL url = new URL("https://192.168.1.5
我正在重写 EventListener 中的 dnsEnd() 函数: @Override public void dnsEnd(Call call, String domainName, L
我有okhttp的android多项目设置,作为多个项目的一部分 在项目A中,okhttp包含在源代码中。 在项目B中,okhttp带有库依赖项的一部分。 构建时,出现以下错误。 Execution
我正在尝试使用pesapal向我的应用程序添加付款但遇到问题,我将它们的依赖实现'com.pesapal.android:pesapalandroid:1.0.5'添加到我的应用程序中,从那时起我就一
我只是尝试使用 OkHttp但是当我将我的 gradle 文件与 Android Studio 同步时,我遇到了以下问题: Error:Failed to find: com.squareup.okh
我是一名优秀的程序员,十分优秀!