如何在android http POST中添加参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3288823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add parameters in android http POST?
提问by UMAR
friends,
朋友们,
i am trying to upload file to php server using following tutorial http://getablogger.blogspot.com/2008/01/android-how-to-post-file-to-php-server.html
我正在尝试使用以下教程将文件上传到 php 服务器 http://getablogger.blogspot.com/2008/01/android-how-to-post-file-to-php-server.html
i dont know how to add parameters like
我不知道如何添加参数
userid="12312";sessionid="234"
userid="12312";sessionid="234"
in it.
在里面。
any one guide me how to achieve this?
任何人指导我如何实现这一目标?
any help would be appreciated.
任何帮助,将不胜感激。
回答by Jorgesys
How to make an http POST and adding parameters.
如何制作 http POST 并添加参数。
how to add parameters? you must have something like.
如何添加参数?你一定有类似的东西。
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("userid", "12312"));
nameValuePairs.add(new BasicNameValuePair("sessionid", "234"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
This is a complete method:
这是一个完整的方法:
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/myexample.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "stackoverflow.com is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}