2016-10-03 187 views
1

我正在尝试发帖。Okhttp post json array

 RequestBody formBody = new FormBody.Builder() 
      .add("userId", userId) 
      .add("patientName", patient.getName()) 
      .add("patientDob", patient.getDOB()) 
      .add("referralFor", patient.getFor()) 
      .add("patientPhoto", "") 
      .add("message", "test") 
      .add("referralParticipants",) 
      .build(); 

但是referralParticipants是一个json数组。这也可能是动态的。我不确定如何做到这一点,因为没有什么形式的数据,它似乎只是原始的JSON被发送?

enter image description here

回答

4

这是你应该如何创建RequestBody媒体类型application/json

声明application/json媒体类型:

public static final MediaType JSON 
     = MediaType.parse("application/json; charset=utf-8"); 

创建request对象:

RequestBody body = RequestBody.create(JSON, jsonStringToBePosted); 
Request request = new Request.Builder() 
         .url(url) 
         .post(body) 
         .build();