0

我正在尝试将字符串组合发布到我的后端服务器。我怎样才能使用BasicNameValuePair. 这是我正在尝试的一些代码:

HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient httpClient = new DefaultHttpClient(params);
HttpPost post = new HttpPost("API HERE");

List<NameValuePair> postData = new ArrayList<NameValuePair>();
postData.add(new BasicNameValuePair("username", "the_username"));
postData.add(new BasicNameValuePair("password", "the_password"));

我想发送用户名和密码,例如:

username=USER&password=PWD

如何实现成功post到服务器。

帮助将不胜感激。

4

1 回答 1

0

以下是我用于所有POST变量的内容:

HttpParams httpParams = new BasicHttpParams();
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);

HttpPost httpPost;
httpPost = new HttpPost(Net_URL + Net_Get);

httpPost.setEntity(new UrlEncodedFormEntity(/*Name Value Pairs*/));

HttpResponse response = httpClient.execute(httpPost);

作为一个附带建议,我会先对用户密码进行某种加密,然后再通过网络发送,我见过很多人不这样做^.^

于 2013-03-03T05:59:41.297 回答