I need to put an array of objects ( each object has 2 fields ) as parameters in url of http request. How can i do it and how should this link looks like?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can make an xml with your structure i.e an array of objects each having two fields then convert that to string as, As an example,
String input = String.format("<Request><Data><Id>%s</Id></Data>
</Request>",studentIdSelected);
Then call this method with input and url as parameters for posting your data,
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;
}
Alternatively you can use json as well.
回答2:
best solution is send http post request in json or xml format .