Here is my restlet client code on android :
public void sendDataToServer(final JsonObject jsonObject) {
new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
String jsonObjectString = jsonObject.toString();
StringEntity entity = null;
try {
entity = new StringEntity(jsonObjectString);
entity.setContentType(APPLICATION_JSON);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ClientResource clientResource = new ClientResource("http://192.168.0.101:8080/RestletDemo/register");
try {
Representation representation = clientResource.post(entity, MediaType.APPLICATION_JSON);
// representation.getJsonObject();
Response response = clientResource.getResponse();
String jsonResponse = response.getEntity().getText();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
This code is on the server-side :
@Post("json")
public String registerNumber(JsonRepresentation entity) {
String serverResponse = "Number Registered";
try {
// JsonRepresentation respresentation = new JsonRepresentation();
String response = this.getRequest().getEntity().getText();
System.out.println(response);
Gson gson = new Gson();
serverResponse = gson.toJson(serverResponse);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return serverResponse;
}
Current scenario : I’m able to send post request to the server. Moreover, I’m also able to recieve response. But, I want to retrieve the data sent from the android device(client), in the POST request, on the server. How do I overcome this issue? Thanks.
I’ve solved the issue. Here is the server code in order to retrieve POST data, sent from the client, on the server side.
You can test this code using curl from the terminal:
curl -i -X POST <your_url> -H "Content-Type: application/json" -d '{"key" : "value"}'