Retrofitアップロードjson

1514 ワード

RequestBodyをパラメータとして渡す
@PUT(Url)
Call getSubmitCheck(@Body RequestBody requestBody);
RequestBody.create(MediaType.parse("application/json; charset=utf-8"),"json"//json     json    

たとえば
Retrofit retrofit = new Retrofit.Builder().baseUrl(URL).client(client).build();
ApiService apiService = retrofit.create(ApiService.class);

//new JSONObject  getMap()        map,                  ,             ,             
//  {"data":{      }},           "data"  
RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"),
                                   "{\"data\":"+new JSONObject(getMap()).toString()+"}");
        Call call = apiService.add(requestBody);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, retrofit2.Response response) {
                Log.d(getClass().getSimpleName(), "onResponse: ----" + response.body().string());
            }

            @Override
            public void onFailure(Call call, Throwable t) {
                Log.d(getClass().getSimpleName(), "onFailure: ------" + t.toString());
            }
        });