How to add parameters in android http POST?

后端 未结 1 1065
盖世英雄少女心
盖世英雄少女心 2020-11-29 05:26

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

<
相关标签:
1条回答
  • 2020-11-29 06:01

    How to make an http POST and adding parameters.

    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
       }
    }
    
    0 讨论(0)
提交回复
热议问题