1944

Setting parameters in HTTP POST

Question:

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(URLString); HttpResponse response = httpClient.execute(httpPost); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("method", "completeUserLogin")); String[] params = new String[]{"100408"}; response = httpClient.execute(httpPost);

I want to add params to nameValuePairs. BasicNameValuePair class does not allow you to add arrays. Any thoughts?

Thanks in advance!

Answer1:

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=new JSONObject(); json.put("method", "completeUserLogin"); JSONArray arr= new JSONArray(); arr.put("100408"); json.put("params", arr); String params=json.toString();

Answer2:

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

Recommend

  • Android Login to Website - Always returns login page data
  • Create this string exactly in Android
  • How can I view the string sent from httppost?
  • Getting error in AsyncTask when using Httpclient and Http core library
  • Axios Http client - How to construct Http Post url with form params
  • 411 Content-Length Required
  • HttpPost in android not working
  • Posting data from android to PHP
  • How to apply async task into this
  • Wrap C++ function using Boost Reflect or another C++ reflection library
  • android Navigation Bar hiding and persantage of usable screen overlap
  • drawing random circles, storing their coorindates in an array
  • Primefaces ManyCheckbox inside ui:repeat calls setter method only for last loop
  • Security issues with PHP's Readfile method
  • onBackPressed() not being executed
  • Rails Find when some params will be blank
  • Web-crawler for facebook in python
  • Delete MySQLi record without showing the id in the URL
  • Akka Routing: Reply's send to router ends up as dead letters
  • Cannot Parse HTML Data Using Android / JSOUP
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • How do I configure my settings file to work with unit tests?
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • Turn off referential integrity in Derby? is it possible?
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Add sale price programmatically to product variations
  • unknown Exception android
  • Can't mass-assign protected attributes when import data from csv file
  • Programmatically clearing map cache
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How do I use LINQ to get all the Items that have a particular SubItem?