【问题标题】:How to add parameters in android http POST?如何在 android http POST 中添加参数?
【发布时间】:2011-03-18 08:36:45
【问题描述】:

朋友们,

我正在尝试使用以下教程将文件上传到 php 服务器 http://getablogger.blogspot.com/2008/01/android-how-to-post-file-to-php-server.html

我不知道如何添加参数

userid="12312";sessionid="234"

在里面。

任何人指导我如何实现这一目标?

任何帮助将不胜感激。

【问题讨论】:

    标签: android


    【解决方案1】:

    如何进行http POST及添加参数。

    如何添加参数?你必须有类似的东西。

    // 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));  
    

    这是一个完整的方法:

    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
       }
    }
    

    【讨论】:

    • 嗨,如果假设我必须将字符串“id”和“stringdata”作为另一个字符串的子项传递,我该如何继续。 (ie) "{"parentNode":{"id":"12345","stringdata": "stackoverflow.com is Cool!"}} 在这你能提一下如何将值传递给“parentNode”
    猜你喜欢
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    相关资源
    最近更新 更多