您好我正在考虑一个测验应用程序,所以我需要调用post rest services(json),下面一个是我的示例代码,它不能正常工作
我需要传递3个参数作为参数1-userid,2-List questionids和3-list answerIds。我的问题是如何在这里传递List Object而不是String或Json String -
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("userid", "121213"));
nameValuePairs.add(new BasicNameValuePair("questionsids", listobj);
nameValuePairs.add(new BasicNameValuePair("answerIds", listObj));
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setHeader(HTTP.CONTENT_TYPE,"application/x-www-form-urlencoded;charset=UTF-8");
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
还有其他方法可以调用帖子休息服务。如何解决上述问题?
由于 兰詹。
答案 0 :(得分:0)
使用for循环并使用相同的键添加。
像这样:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); for (ModelAssignee assignee : assignmentChips.getAssignees()) { nameValuePairs.add(new BasicNameValuePair("AssignedToIds", String.valueOf(assignee.Id))); }