Skip to content Skip to sidebar Skip to footer

Setting Parameters In Http Post

I am trying make a JSON call to a server using HttpClient API. The code sinppet is shown below. HttpClient httpClient = new DefaultHttpClient(); HttpGet httpPost = new HttpPost(URL

Solution 1:

Look at this . Here they pass the array in BasicNameValuePairs.Here the colours is the array which we are going to send on a server. You should use your array varible instead of colours.

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
nameValuePairs.add(new BasicNameValuePair("colours[0]","red"));  
nameValuePairs.add(new BasicNameValuePair("colours[1]","white"));  
nameValuePairs.add(new BasicNameValuePair("colours[2]","black"));  
nameValuePairs.add(new BasicNameValuePair("colours[3]","brown"));  

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpClient.execute(httpPost);

Solution 2:

If you are posting data in json format then you should not post params like this. Instead create a JSONObject put these values in that json object, and get a string from that json object and then create a StringEntity, and set this Entity to HttpPost object.

Creating JSONObject for the Request:

JSONObject json=newJSONObject();
json.put("method", "completeUserLogin");
JSONArray arr= newJSONArray();
arr.put("100408");
json.put("params", arr);

String params=json.toString();

Post a Comment for "Setting Parameters In Http Post"