2016-10-03 183 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();