2017-02-21 160 views
-1

我使用okhttp3.RequestBody发送请求到服务器, 如果我有JSONObject的数据我需要发送 我写这样的代码:如何从JSONObject创建RequestBody?

RequestBody requestBody = new MultipartBody.Builder() 
           .setType(MultipartBody.FORM) 
           .addFormDataPart("id", object.optLong(Comment.TASK_ID_JSON_TAG) + "") 
           .addFormDataPart("type", "IMAGE") 
           .addFormDataPart("latitude", object.optDouble(Comment.LATITUDE_JSON_TAG) + "") 
           .addFormDataPart("longitude", object.optDouble(Comment.LONGITUDE_JSON_TAG) + "") 
           .build(); 

现在如果我有JSONObject的大数据有一种直接创建RequestBody的方法?
感谢您的帮助。

+0

,请阅读以下:[第1页](http://stackoverflow.com/help/how-to-ask)[第2页](http://stackoverflow.com/帮助/ mcve)[page3](http://stackoverflow.com/help/on-topic) –

+0

好的,我做了,我的问题有什么问题? 不清楚吗? – Tefa

+0

是的,我不明白,我通过任何人都会明白你的需要。相反,请提供你的工作和陈述你想要达到的目标 –

回答

0

也许你可以在一个参数中发布所有的json对象,并将其发送到服务器。

检查了这一点https://stackoverflow.com/a/34180100/1067963

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

OkHttpClient client = new OkHttpClient(); 

String post(String url, String json) throws IOException { 
    RequestBody body = RequestBody.create(JSON, json); 
    Request request = new Request.Builder() 
     .url(url) 
     .post(body) 
     .build(); 
    Response response = client.newCall(request).execute(); 
    return response.body().string(); 
} 
+0

谢谢,我会试试看 – Tefa