2

I am implementing an android app in which I want to use some methods from a server (which was not implemented by me). Now when I try to make an http-post where I have to pass only String parameters everything works fine with a code like:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

nameValuePairs.add(new BasicNameValuePair("user[email]", email));
nameValuePairs.add(new BasicNameValuePair("user[password]", password));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

ResponseHandler<String> responseHandler=new BasicResponseHandler();
String responseBody = httpclient.execute(httppost, responseHandler);
JSONObject response=new JSONObject(responseBody);   
System.out.println("RESPONSE " + response.toString());

I get the response as a json object which I can easily use to take the attributes I wish.

Now there are methods that require non String values (integer, or boolean) as parameters. I cannot pass these arguments in a list such as List<NameValuePair> since this takes only Strings. I tried to pass it as a json object too but no success.

So my first question is if it is possible to have non String parameters in http post? And if yes, how should it be done? Eg if in the code above email was an integer and password a Boolean (in shake of the example), how should I handle them?

Thank you all in advance!

Josh Lee
  • 149,877
  • 34
  • 253
  • 263
george
  • 1,366
  • 5
  • 22
  • 38
  • Just passs their string representations? – Andrew Barber Mar 30 '12 at 18:15
  • you mean like "1" for 1? But the server waits for an integer I think. So I get an IO exception which I think is because of this.. Isn't there some method to pass non String or http-post does not allow it? – george Mar 30 '12 at 18:20
  • Is there a functioning web page that posts to the server resource you're trying to use? If so, can you perhaps use something like the HttpFox plugin for Firefox, or another traffic-sniffer, to see what the successful HTTP request looks like and imitate its format? – Joshua Carmody Mar 30 '12 at 19:00
  • No this is a server that some guys made for a project I am supposed to make the client.. I am the first to use it. I may ask them to receive everything as Strings and work it from their side (parse integer from string etc), but I want insight to understand if I am wrong and it can be done through http-post and non Strings parameters. – george Mar 30 '12 at 19:07

2 Answers2

0

Sure, consider a file upload.

The file is binary (ok, consider uploading a picture if someone consider a text not to be binary enough)

the technique is a http-post

stefan bachert
  • 8,643
  • 3
  • 31
  • 38
  • yes I thought of that.. But I do not know if everything is sent as a string and the decoded from the server.. So how should I pass the non strings params? – george Mar 30 '12 at 18:30
  • 1
    this link is very usefull. However, it uses urlconnection to communicate http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests – stefan bachert Mar 30 '12 at 18:46
  • You must know the api and contract for the resource you are getting/posting to. – Shellum Mar 30 '12 at 18:58
0

All http request parameters will come in as strings, but the server side code can convert them. The server could for example grab a JSON string from a request parameter and turn it into an object that contains any amount of serialized data. This could include integers, lists etc.

The implementation though will be dependent on that server side code. Both the client and server for example could use GSON to send objects and lists back and forth.

public void doPost(...)
{
    String param = request.getParameter("someParam");
    MyCustomObject myCustomObject = (MyCustomObject)gson.fromJson(param, MyCustomObject.class);
}
Shellum
  • 3,059
  • 2
  • 18
  • 26
  • So in this code you grab the parameter that is requested from the server as a string and then you form it as any other type you want? And how to pass it to the server after that? I am confused.. – george Mar 30 '12 at 18:35
  • "All http request parameters will come in as strings" that is wrong for post var. Only url parameter will be "string" – stefan bachert Mar 30 '12 at 18:38
  • All request parameters, as shown in the example above, that you can access via request.getParameter come in as strings: http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html – Shellum Mar 30 '12 at 18:59