Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add parameters in android http POST?

Tags:

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

i dont know how to add parameters like

userid="12312";sessionid="234"

in it.

any one guide me how to achieve this?

any help would be appreciated.

like image 677
UMAR-MOBITSOLUTIONS Avatar asked Jul 20 '10 09:07

UMAR-MOBITSOLUTIONS


People also ask

How do I add parameters in HTTP request?

We can add parameters using String name-value pairs, or utilize NameValuePairs class for that purpose. Similarly, UriBuilder can be used to add parameters to other HttpClient request methods.

Can we send parameters in POST request?

In a POST request, the parameters are sent as a body of the request, after the headers. To do a POST with HttpURLConnection, you need to write the parameters to the connection after you have opened the connection.

What is a parameter in Android?

A Parameter provides information about method parameters, including its name and modifiers. It also provides an alternate means of obtaining attributes for the parameter.


1 Answers

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    } } 
like image 135
Jorgesys Avatar answered Oct 16 '22 13:10

Jorgesys