How to POST raw whole JSON in the body of a Retrofit request?

前端 未结 23 2358
面向向阳花
面向向阳花 2020-11-22 00:57

This question may have been asked before but no it was not definitively answered. How exactly does one post raw whole JSON inside the body of a Retrofit request?

See

23条回答
  •  广开言路
    2020-11-22 01:48

    use following to send json

    final JSONObject jsonBody = new JSONObject();
        try {
    
            jsonBody.put("key", "value");
    
        } catch (JSONException e){
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),(jsonBody).toString());
    

    and pass it to url

    @Body RequestBody key
    

提交回复
热议问题