简体   繁体   English

如何使用 retrofit 发送带有键值对的数组数组

[英]How can i send array of array with key value pairs with retrofit

Here is what i want to send in a post request with retrofit any idea?这是我想通过 retrofit 发送的帖子请求中的任何想法?

"quiz_data" : [ ['question_id' => 1, 'option_id' => 2], ['question_id' => 2, 'option_id' => 3], ['question_id' => 3, 'option_id' => 2] ] 

It has to be dynamic as I don't know how many quiz questions will be added from the admin.它必须是动态的,因为我不知道管理员会添加多少测验问题。

I think you can create a Call with a Map object that has the values that you need.我认为您可以使用具有所需值的 Map object 创建呼叫。


@POST("{endpoint}")
Call<Void> sendData(@Body Map<QuestionID,OptionID> dataToBeSend);

more information look at this keep coding.更多信息看这个继续编码。

Try this code试试这个代码

ArrayList<ArrayList<HashMap<String, String>>> hashMapArrayList1 = new ArrayList<>();
ArrayList<HashMap<String, String>> hashMapArrayList2 = new ArrayList<>();
    for (int i = 0; i < yourlist.size(); i++) {
          try {
                HashMap<String, String> hs = new HashMap<>();
                hs.put("question_id", "" + yourlist.get(i).getQueId().trim());
                hs.put("option_id", "" + yourlist.get(i).getOptionId().trim());                
                hashMapArrayList2.add(hs);
          } catch (Exception e) {
                e.printStackTrace();
          }
        }
      hashMapArrayList1.add(hashMapArrayList2);
      paramas.put("quiz_data", hashMapArrayList1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM