我需要将一些数据从我的 Android 设备发送到我的服务器。我正在通过 JSON 执行此操作。我已经在 Android 上实现了 JSON 帖子,并且我正在尝试在服务器端进行映射以检索该数据。我的问题是我不断收到一个空字符串。
用于发送 JSON 的 Android 方法:
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();
}
服务器端方法:
@RequestMapping(value = "/getLatestCalls", method = RequestMethod.POST)
public void getData(@ModelAttribute String json){
//... do something
}
问题是在这种方法中,我的json String 每次都是“”。我也尝试过使用@RequestParam
,但它不再进入该方法。我也试过了@ModelAttribute("json")
。
有人可以在这里给我一点启发吗?先感谢您。