I need to send some data from my Android device to my server. I am doing this through JSON. I have implemented the JSON post on Android, and I am trying to do a mapping on the server side in order to retrieve that data. My problem is that I keep getting an empty string.
Android method used to send JSON:
private void sendJson(final String json, final String URL) {
Thread t = new Thread(){
public void run() {
Looper.prepare(); //For Preparing Message Pool for the child Thread
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
try{
HttpPost post = new HttpPost(URL);
StringEntity se = new StringEntity(json);
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
client.execute(post);
}
catch(Exception e){
e.printStackTrace();
}
Looper.loop(); //Loop in the message queue
}
};
t.start();
}
Server-side method:
@RequestMapping(value = "/getLatestCalls", method = RequestMethod.POST)
public void getData(@ModelAttribute String json){
//... do something
}
The thing is that in this method my json String is “” every time. I have also tried using @RequestParam but with that it doesn’t enter the method anymore. I have also tried with @ModelAttribute("json").
Can someone enlighten me a little here? Thank you in advance.
Here is the solution and it works fine.
server-side
client-side