我需要使用okhttp java将这样的数组(从后端dev获取)发送到服务器,任何想法? 得到一些关于它的文献会更好
[answers] => Array
(
[0] => Array
(
[answer_id] => 4648
[duration_time_question] => 0
[text_open_answer] =>
)
[1] => Array
(
[answer_id] => 4650
[duration_time_question] => 0
[text_open_answer] =>
)
[2] => Array
(
[answer_id] => 4667
[duration_time_question] => 1
[text_open_answer] =>
)
[3] => Array
(
[answer_id] => 4652
[duration_time_question] => 1
[text_open_answer] =>
)
[4] => Array
(
[answer_id] => 4678
[duration_time_question] => 0
[text_open_answer] =>
)
我已经尝试了很多变种来发送一个数组,但都没有工作( 我在需要的部分使用此代码
OkHttpClient client = new OkHttpClient();
client.newCall(new DoRequest().Post(dictionary, getIntent().getExtras().getString("url"), array))
.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
System.out.println("Failed");
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
String temp = response.body().string();
System.out.println("Success " + temp);
}
});
这是受支持的请求类。一方面,当我以键值形式发送数据时它起作用,但另一方面它不会通过for发送我的数组。
public Request Post(Dictionary<String, String> dictionary, String url, Dictionary<String, String> data) {
RequestBody formBody = null;
Dictionary<String, String> array = new Hashtable<>();
for (int i = 0; i < array.size() / 3; i++) {
formBody = new FormBody.Builder()
.add("answers["+i+"]", array.get("answer_id"))
.add("answers["+i+"]", array.get("duration_time_question"))
.add("answers["+i+"]", array.get("text_open_answer"))
.build();
}
break;
}
return new Request.Builder()
.url(url)
.post(formBody).build();
}