我需要将一个对象数组(每个对象有 2 个字段)作为 http 请求的 url 中的参数。我该怎么做以及这个链接应该是什么样子?
您可以使用您的结构创建一个 xml,即一个对象数组,每个对象都有两个字段,然后将其转换为字符串,如下所示:
举个例子,
String input = String.format("<Request><Data><Id>%s</Id></Data>
</Request>",studentIdSelected);
然后使用 input 和 url 作为发布数据的参数调用此方法,
public static String retriver(String Url, String input) {
String responseString = null;
StringEntity stringEntity;
HttpPost postRequest = new HttpPost(Url);
try {
Log.e("string is", input + "\n" + Url);
stringEntity = new StringEntity(input, "UTF-8");
stringEntity.setContentType("application/atom+xml");
postRequest.setEntity(stringEntity);
Log.v("Post", "Posted");
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(postRequest);
HttpEntity getResponseEntity = response.getEntity();
responseString = EntityUtils.toString(getResponseEntity);
} catch (Exception e) {
// TODO: handle exception
postRequest.abort();
Log.w("HttpPostRetreiver", "Error for URL " + Url, e);
}
return responseString;
}
或者,您也可以使用 json。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)