你的HttpParams用於創建HttpEntity HttpEntityEnclosedRequestBase對象上設置,然後你可以有一個列表返回使用下面的代碼
final HttpPost httpPost = new HttpPost("http://...");
final ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("a_param", username));
params.add(new BasicNameValuePair("a_second_param", password));
// add the parameters to the httpPost
HttpEntity entity;
try
{
entity = new UrlEncodedFormEntity(params);
httpPost.setEntity(entity);
}
catch (final UnsupportedEncodingException e)
{
// this should never happen.
throw new IllegalStateException(e);
}
HttpEntity httpEntity = httpPost.getEntity();
try
{
List<NameValuePair> parameters = new ArrayList<NameValuePair>(URLEncodedUtils.parse(httpEntity));
}
catch (IOException e)
{
}
我做了一個類似的事情,只是爲了從URI獲取參數(這是一個Groovy片段,在Java中也是如此): 'def uri = new URI(「https://www.yahoo.com?foo =「bar」) List parameters = new ArrayList (URLEncodedUtils.parse(uri,「UTF-8」)); parameters.each {參數 - > println parameter.name +「:」+ parameter.value}' 這是一種體面的方式來解構請求的參數,而不會搞亂HttpParams對象,除非你準確的知道你想要什麼。 –
2012-11-01 19:16:21