Java 在Spring控制器方法中未获取JSON值
我需要将一些数据从我的Android设备发送到我的服务器。我是通过JSON实现的。我已经在Android上实现了JSON post,我正在尝试在服务器端进行映射以检索该数据。我的问题是我一直得到一个空字符串 用于发送JSON的Android方法:Java 在Spring控制器方法中未获取JSON值,java,android,json,spring,modelattribute,Java,Android,Json,Spring,Modelattribute,我需要将一些数据从我的Android设备发送到我的服务器。我是通过JSON实现的。我已经在Android上实现了JSON post,我正在尝试在服务器端进行映射以检索该数据。我的问题是我一直得到一个空字符串 用于发送JSON的Android方法: private void sendJson(final String json, final String URL) { Thread t = new Thread(){ public void run() {
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字符串都是“”。我也尝试过使用@RequestParam
,但它不再输入该方法。我还尝试了@modeldattribute(“json”)
有人能给我点启发吗?提前感谢。尝试使用。它应该会起作用 这是解决方案,效果很好 服务器端
客户端
public static void httptest() {
ArrayList<TravellingData> tdArray = new ArrayList<TravellingData>();
Gson gson = new Gson();
String jsonString = "";
for (int i = 0; i < 1; i++) {
tdArray.add(ObjectCreater.createMockTravellingDataObject());
}
jsonString = gson.toJson(tdArray);
HttpClient client = new DefaultHttpClient();
HttpPost post = null;
try {
post = new HttpPost(
"http://localhost:8080/uygulama/clientdatacollector");
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("gpsdata", jsonString));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = null;
try {
response = client.execute(post);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
publicstaticvoidhttptest(){
ArrayList tdArray=新的ArrayList();
Gson Gson=新的Gson();
字符串jsonString=“”;
对于(int i=0;i<1;i++){
add(ObjectCreater.createMockTravelingDataObject());
}
jsonString=gson.toJson(tdArray);
HttpClient=new DefaultHttpClient();
HttpPost=null;
试一试{
post=新的HttpPost(
"http://localhost:8080/uygulama/clientdatacollector");
}捕获(URISyntaxException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
试一试{
List nameValuePairs=新的ArrayList(1);
添加(新的BasicNameValuePair(“gpsdata”,jsonString));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse响应=null;
试一试{
响应=client.execute(post);
}catch(httpe异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
BufferedReader rd=新的BufferedReader(新的InputStreamReader(
response.getEntity().getContent());
字符串行=”;
而((line=rd.readLine())!=null){
系统输出打印项次(行);
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
谢谢!我已将设备端的实体更改为nameValuePair,并且成功了。
public static void httptest() {
ArrayList<TravellingData> tdArray = new ArrayList<TravellingData>();
Gson gson = new Gson();
String jsonString = "";
for (int i = 0; i < 1; i++) {
tdArray.add(ObjectCreater.createMockTravellingDataObject());
}
jsonString = gson.toJson(tdArray);
HttpClient client = new DefaultHttpClient();
HttpPost post = null;
try {
post = new HttpPost(
"http://localhost:8080/uygulama/clientdatacollector");
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("gpsdata", jsonString));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = null;
try {
response = client.execute(post);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}