Blog

Android – Restlet or how to have your client working and how to send params in a post request

When you are reading the title, well it seems to be a really easy task to be able to do a POST request with some parameters sent along…

Well it is easy, but man it took me long to actually find this out. There is a Restlet documentation but it lacks a LOT of simple examples for a quickstart.
So, here in this first post of the Restlet serie (I’m sure I will have more to cover) I will show you how to do so…
Here I am getting back the response as a JSON string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// this is if your device doesn't support IPV6
System.setProperty("java.net.preferIPv6Addresses", "false");

ClientResource clientResource = new ClientResource(Method.POST, YOUR_URL);
Form form = new Form();
form.add("param1", param1);
form.add("param2", param2);
Representation entity = clientResource.post(form);
JSONObject json;
try
{
    json = new JsonRepresentation(entity).getJsonObject();
    System.out.println(json.toString());
}
catch (JSONException e)
{
}
catch (IOException e)
{
}

Another tip, be sure you have an internet connection if you using the android emulator, and if you want to use a proxy check this other post.

  • http://twitter.com/jlouvel Jerome Louvel

    Nice post. If you want to contribute to the Restlet user guide/wiki, that would be great!

  • zeflasher

    Hello Jerome.

    Thanks for the feedback. Well I am just starting working with your framework and will continue doing post here but as soon I will have aggregated enough knowledge I will be more than happy to contribute to the wiki with resource I could have posted here and more.

    Bonne journee ;)
    ++