gpt4 book ai didi

java - 如何创建具有 'Content-Type' 、授权 token 和正文的 Volley header ?

转载 作者:行者123 更新时间:2023-12-01 18:58:53 25 4
gpt4 key购买 nike

我正在尝试使用自定义服务器和 oauth 2.0 使用身份验证 token 创建登录 Volley 发布请求。正文:

{
"customer": {
"email": "example@gmail.com",
"password":"12345"
}
}

我试过了

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
final Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer .....UzI1NiIsInR5cCI6IkpXVCJ9.....");
headers.put("email",username.toString());
headers.put("password",password.toString());
headers.put("customer",headers.toString());

Log.e("Json created", headers.toString());
return headers;
}

最佳答案

如果您想将数据作为 JSON 正文传递,请这样做..

String body = "{\"customer\":{\"email\":"+ username + ",\"password\":"+ password +"}}";

StringRequest stringRequest = new StringRequest(Request.Method.POST, url, response -> {
//response here
}, error -> {
//exceptions here
}) {
@Override
public byte[] getBody() throws AuthFailureError {
return body.getBytes();
}

@Override
public HashMap<String, String> getHeaders() throws AuthFailureError {
final HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer .....UzI1NiIsInR5cCI6IkpXVCJ9.....");
return headers;
}
};

关于java - 如何创建具有 'Content-Type' 、授权 token 和正文的 Volley header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59653608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com