【发布时间】:2012-10-26 20:01:58
【问题描述】:
我需要在 http 请求的 url 中放置一个对象数组(每个对象有 2 个字段)作为参数。我该怎么做?这个链接应该是什么样子?
【问题讨论】:
标签: android http httprequest
我需要在 http 请求的 url 中放置一个对象数组(每个对象有 2 个字段)作为参数。我该怎么做?这个链接应该是什么样子?
【问题讨论】:
标签: android http httprequest
您可以使用您的结构创建一个 xml,即一个对象数组,每个对象有两个字段,然后将其转换为字符串, 例如,
String input = String.format("<Request><Data><Id>%s</Id></Data>
</Request>",studentIdSelected);
然后以输入和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。
【讨论】:
最好的解决方案是以 json 或 xml 格式发送 http post 请求。
【讨论】: