gpt4 book ai didi

java - Spring-MVC & Android : Server gets a empty JSON string from android

转载 作者:行者123 更新时间:2023-12-01 11:53:36 25 4
gpt4 key购买 nike

我正在开发一个 Android 项目,我想在其中创建一个到基于 Spring-MVC 的服务器的 RESTful POST 连接。我最初尝试发布一个对象,但经常遇到错误。这就是我尝试发送 JSON 对象的原因。目前我在 Android 应用程序中没有收到任何错误,但是当我收到 JSON 对象并获取字符串时,JSON 对象中没有任何内容。我调试了代码以查看值是否正确发送。我不知道我做错了什么。你能帮忙的话,我会很高兴。非常感谢。

发送对象的 Android 代码:

@Override
public void addRestaurant(Restaurant restaurant) {
Log.d("Restaurant Name",restaurant.getRestaurantName());
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {

Looper.prepare();
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(),10000);
HttpResponse response;
JSONObject jsonObject = new JSONObject();

HttpPost post = new HttpPost(url);
jsonObject.put("restaurantName",restaurant.getRestaurantName());
jsonObject.put("postLeitZahl",restaurant.getPostLeitZahl());
jsonObject.put("phoneNumber",restaurant.getPhoneNumber());
jsonObject.put("id",restaurant.getId());

StringEntity stringEntity = new StringEntity(jsonObject.toString());
stringEntity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/JSON"));
post.setEntity(stringEntity);
response = client.execute(post);

} catch (Exception e){
e.printStackTrace();
}
Looper.loop();
//String response = restTemplate.postForObject(url,restaurant,String.class);
//Log.d(response,"Response from webserver is");
}
});
thread.setPriority(Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
}
}

Spring Controller 代码:

    @RequestMapping(value = "/restaurant/add",method = RequestMethod.POST,consumes="application/json")
@ResponseBody
public String addRestaurantWebView(JsonObject restaurant){
System.out.println(restaurant.getAsString());
return "true";
}

我不知道我做错了什么,并且我无法找到一些可以告诉我如何根据 android 中的代码配置服务器的资源,反之亦然。非常感谢..:-)

编辑(解决方案)(部分Java对象)

由于我的初衷是发送一个失败的 Java 对象,所以我恢复为 JSON,但后来它与 Java 一起工作,这里是对我有用的 Android 代码和 Spring-MVC Controller 和 bean。

Android 代码:

package com.example.myapp;

import android.os.Process;
import android.util.Log;
import org.springframework.http.*;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

public class RestaurantServiceImpl implements RestaurantService {

String url = "http://192.168.178.40:8080/restaurant/add";

@Override
public void addRestaurant(Restaurant restaurant) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity entity = new HttpEntity(restaurant,headers);
ResponseEntity<String> out = restTemplate.exchange(url, HttpMethod.POST,entity,String.class);
Log.d(out.toString(),"Response from server");
} catch (Exception e){
e.printStackTrace();
}
}
});
thread.setPriority(Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
}
}

Spring-MVC Controller :

 @RequestMapping(value = "/restaurant/add",method = RequestMethod.POST)
@ResponseBody
public String addRestaurantWebView(@RequestBody Restaurant restaurant){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("restaurant", new Restaurant());
modelAndView.addObject(restaurant);
this.restaurantService.addRestaurant(restaurant);
return "true";
}

Servlet-context.xml

添加此:

<mvc:annotation-driven />

<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:ref bean="jsonMessageConverter"/>
</beans:property>
</beans:bean>

<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>

最佳答案

这就是我要做的:在 Spring 应用程序上创建 Restaurant 类。然后将其作为请求映射中的参数与@ModelAttribute一起使用:

public String addRestaurantWebView(@ModelAttribute Restaurant restaurant) {

然后,在 Android 上使用 MultipartEntity 发送参数:

Charset charset = Charset.forName("UTF-8");
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, charset);
entity.addPart("restaurantName", new StringBody(restaurant.getRestaurantName(), charset));
entity.addPart("postLeitZahl", new StringBody(restaurant.getPostLeitZahl(), charset));
entity.addPart("phoneNumber", new StringBody(restaurant.getPhoneNumber(), charset));
entity.addPart("id", new StringBody(restaurant.getId(), charset));

HttpPost post = new HttpPost(url);
post.setEntity(entity);
response = client.execute(post);

关于java - Spring-MVC & Android : Server gets a empty JSON string from android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28606261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com