[英]How to make an Okhttp Request with “Content-type:application/x-www-form-urlencoded”?
I have an api requirement of Sending following parameters in header- 我有一个在标头中发送以下参数的api要求:
Content-Type - application/x-www-form-urlencoded 内容类型-应用程序/ x-www-form-urlencoded
authKey- (Session token) authKey-(会话令牌)
and following parameters in body(form day ie key-value pair) 以及正文中的以下参数(表单日期,即键值对)
storeId -1 storeId -1
type -Product 类型-产品
CategoryId -324 类别编号-324
But whenever I hit this api, I am always getting 401(UnAuthorized) error. 但是,每当我按下此api时,我总是会收到401(UnAuthorized)错误。 I have tried using MultipartRequest body and formBody, I know this is nothing to do with the body(Its the header where I need to send the Content-Type and authKey). 我试过使用MultipartRequest正文和formBody,我知道这与正文无关(它是我需要发送Content-Type和authKey的标头)。 Below is my code- 以下是我的代码-
Request.Builder requestBuilder = new Request.Builder();
requestBuilder.addHeader("Content-Type", "application/x-www-form-urlencoded");
requestBuilder.addHeader("authKey",AppSharedPref.getTokenMobikul(context));
RequestBody formbody = new FormBody.Builder().add("CategoryId",bodyparms.get(0)).
add("type",bodyparms.get(1)).build();
requestBuilder.post(formbody);
The Same api is giving Response with retrofit library So how to achieve this using Okhttp. Same api提供了带有改造库的Response,因此如何使用Okhttp实现这一点。
Might this will help 可能会有所帮助
FormBody.Builder formBuilder = new FormBody.Builder()
.add("key", "value");
// add more parameter as follow:
formBuilder.add("mobile", "9999999999");
RequestBody formBody = formBuilder.build();
Request request = new Request.Builder()
.url("https://www.hittheserver.com")
.post(formBody)
.build();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.