我在截击请求参数中发送了一些键和值。我能够成功发送所有数据,但是,我想以这种格式发送一个ArrayList
params.put("facilities", "facilities=1&facilities=3&facilities=7");
我以1、3和7为例,这将包括选定的元素并位于我的设施ArrayList中。我在屏幕上有8个项目,如果我选择4个,则ArrayList将包含4个项目,但是如何以这种确切的params格式发送?我也尝试了下面的代码,但是也没有用
for (int i=0; i<facilitiesList.size(); i++){
params.put("facilities", facilitiesList.get(i).toString());
}
我想以我上面显示的格式发送所有选定的数据。有人知道如何解决吗?
编辑-我使用下面的代码选择项目
private List<Integer> facilitiesList; ///initialization
private BaseRecyclerAdapter<FacilitiesApi.ApiResponse.Datum, FacilitiesViewHolder> mBaseRecyclerAdapter = new BaseRecyclerAdapter<FacilitiesApi.ApiResponse.Datum, FacilitiesViewHolder>(FacilitiesViewHolder.class, R.layout.layout_list_item_facilities) {
@Override
protected void populateViewHolder(final FacilitiesViewHolder viewHolder, final FacilitiesApi.ApiResponse.Datum model, final int position) {
viewHolder.mCustomCheckBoxFacilities.setText(model.getName());
if (facilitiesList != null) {
if (facilitiesList.contains(model.getId())) {
viewHolder.mCustomCheckBoxFacilities.setChecked(true);
} else {
viewHolder.mCustomCheckBoxFacilities.setChecked(false);
}
}
viewHolder.mCustomCheckBoxFacilities.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
facilitiesList.add(Integer.valueOf(model.getId()));
} else {
facilitiesList.remove(Integer.valueOf(model.getId()));
}
}
});
}
};
答案 0 :(得分:1)
您应该先使用for循环创建请求字符串,然后在请求中发送它。 你应该做这样的事情。
String facilities="";
for (int i=0; i<facilitiesList.size(); i++){
if(i==0){
facilities=facilities+"facilities="+facilities.get(i);
}else{
facilities=facilities+"&facilities="+facilities.get(i);
}
}
需要i==0
检查才能停止在第一个位置添加&符号。请尝试执行此操作