2013-10-24 99 views
3

我想向webApi網址發出請求,您已經編寫了以下代碼,並且我在NameValuePair對象中有我的參數。如何將URL參數從NameValuePair添加到HttpPost請求

現在我不知道如何將這些參數添加到基礎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; 
} 

回答

3

您可以使用此參數添加到URL

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

    url +=UrlString;