我使用的Android API,使用HTTP POST方法来发送一些数据:如何在Android中发布?
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://myapp.com/");
try {
List parameters = prepareHttpParameters();
HttpEntity entity = new UrlEncodedFormEntity(parameters);
httppost.setEntity(entity);
ResponseHandler responseHandler = new BasicResponseHandler();
response = httpclient.execute(httppost, responseHandler);
Toast.makeText(this, response, Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO: manage ClientProtocolException and IOException
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
,并在这里准备我的参数:
List parameters = new ArrayList(2);
parameters.add(new BasicNameValuePair("usr", "foo"));
parameters.add(new BasicNameValuePair("pwd", "bar"));
return parameters;
但它似乎是错误的,因为我没有得到任何预期的回应。
我已经用相同的参数使用Curl测试了相同的请求,并且我得到了预期的响应。
我的代码错了吗?
非常感谢您
2011-01-20
Israel
+0
**可能的复制**为http:// stackoverflow.com/questions/4470936/how-to-do-a-http-post-in-android/4472300#4472300 –
2011-01-20 09:14:26