代码之家  ›  专栏  ›  技术社区  ›  UMAR-MOBITSOLUTIONS

如何在androidhttppost中添加参数?

  •  21
  • UMAR-MOBITSOLUTIONS  · 技术社区  · 14 年前

    朋友,

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

    我不知道如何添加参数,如

    在里面。

    有人指导我如何做到这一点吗?

    1 回复  |  直到 7 年前
        1
  •  66
  •   Community CDub    4 年前

    如何制作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
       }
    }