我正在尝试将JSONArray从Android传递到我的Spring java服务器。
我已经尝试了两种方法。一种是将JSONArray作为String传递并使用@PathVariable注释进行捕获。
这样我可以得到[{“id”:6,“numDishes”:1,“观察”:“假”},{“id”:2,“numDishes”:3,“观察”:“false “}}我猜我可以这样做。
我用的代码是:
在android中
HttpGet request = new HttpGet(serverURL + action);//action already has two params
HttpResponse response = httpclient.execute(target, request);
在服务器到达/ orderService / addOrder / 1 / [{“id”:6,“numDishes”:1,“观察”:“false”},{“id”:2,“numDishes”:3,“观察 “:” 假“}]
@RequestMapping(method=RequestMethod.GET, value="/addOrder/{tableNumber}/{jsonParam}")
public void addOrder(@PathVariable Integer tableNumber, @PathVariable String jsonParam) {
log.info("String encoded: " + jsonParam);
}
无论如何,我更愿意直接这样做。像
这样的东西的Android
HttpPost request = new HttpPost(serverURL + action + URLEncoder.encode(paramsString, "UTF-8"));
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
HttpResponse response = httpclient.execute(target, request);
服务器
public class OrderPojo extends ArrayList<DishPojo>{}
@RequestMapping(method=RequestMethod.POST, value="/addOrderPost/{tableNumber}/{jsonParam}")
public void addOrderPost(@PathVariable Integer tableNumber, @RequestBody OrderPojo jsonParam) {
log.info("addOrderPost OrderPojo: " + jsonParam);
}
另外,因为我认为它可能是问题的一部分,所以我在spring-servlet中有这个:
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter"/>
</list>
</property>
</bean>
<mvc:annotation-driven/>
我是在正确的道路上吗?如何将我的对象直接解析为服务器内的自定义对象?
答案 0 :(得分:1)
这是解决方案
//Android Client. Calling the server
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
JSONObject jo = new JSONObject();
jo.put("x1", UtilSettings.getTableNumberPreference(context));
jo.put("x2", createJSONObject());
nameValuePairs.add(new BasicNameValuePair("xxx", jo.toString()));
StringEntity en = new StringEntity(nameValuePairs.get(0).getValue());
en.setContentEncoding(new BasicHeader(HTTP.CONTENT_ENCODING, HTTP.UTF_8));
en.setContentType("application/json");
request.setEntity(en);
request.setHeader(HTTP.CONTENT_TYPE, "application/json");
HttpResponse response = httpclient.execute(target, request);
result = EntityUtils.toString(response.getEntity());
//Server side
public class Object1Pojo{
private Integer x1;
private List<DishPojo> x2;
}
public class Object2Pojo {
private String x1;
private String x2;
private String x3;
}
@RequestMapping(method=RequestMethod.POST, value="/addObjectPost", consumes="application/json")
public @ResponseBody void addObjectPost(@RequestBodyObject1Pojo o1) {
log.info(o1);
}
//XML
<mvc:annotation-driven>
<mvc:message-converters>
<!-- converts @ResponseBody String return types into the response body -->
<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>
<!-- standard form encoding -->
<bean id="formHttpMessageConverter" class="org.springframework.http.converter.FormHttpMessageConverter"/>
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> -->
<bean id="jsonViewResolver" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver" />