繁体   English   中英

如何创建具有“内容类型”、授权令牌和正文的截击标题?

[英]How to create volley header having 'Content-Type', Authorization Token and body?

我正在尝试使用带有 oauth 2.0 的自定义服务器使用身份验证令牌创建登录 volley post 请求。 身体:

{
"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;
    }
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM