如何通过在Okhttp中传递对象来发出POST请求?

时间:2017-12-14 10:49:05

标签: java android json http-post okhttp

这是我的Json身体:

{
    "username": {
        "country": "IN", 
        "number": "9620494812"
    }, 
    "password": "119209"
}

我尝试使用Okhttp发出POST请求,如下所示:

Username username = new Username("IN" , "9620494812");

JSONObject postData = new JSONObject();

try {
    postData.put("username" , username);
    postData.put("password" , "119209");

} catch (JSONException e) {
    e.printStackTrace();
}

RequestBody body = RequestBody.create(MEDIA_TYPE , postData.toString());

Request request = new Request.Builder().url("https://www.gruppie.in/api/v1/login")
        .method("POST" , body).build();

我已检查过请求,但我仍然收到错误

回应是:

{"status":401,"type":"about:blank","title":"Unauthorized"}

我在这里做错了什么?

1 个答案:

答案 0 :(得分:4)

根据您的JSON,您必须发送UpdateSourceTrigger这样的请求。试试这个

JSON
相关问题