如何将NameValuePair中的URL参数添加到HttpPost请求中

时间:2013-10-24 00:47:03

标签: java android api

我正在尝试向webApi网址发出请求,您已编写以下代码,我的参数位于NameValuePair对象中。

现在我不知道如何将这些参数添加到基础uri我是否必须通过连接字符串手动完成?还是有其他办法,请帮忙。

private static final String apiBaseUri="http://myapp.myweb.com/path?";

private boolean POST(List<NameValuePair>[] nameValuePairs){
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(apiBaseUri); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs[0]));
    HttpResponse response = httpclient.execute(httppost);
    String respond = response.getStatusLine().getReasonPhrase();
    Log.d("MSG 3 > ",respond);
    return true;
}

1 个答案:

答案 0 :(得分:3)

您可以使用它将参数添加到网址

 nameValuePairs.add(new BasicNameValuePair("name",value));
  String UrlString = URLEncodedUtils.format(nameValuePairs, "utf-8");

   url +=UrlString;