0

我正在尝试将 JSONArray 从 Android 传递到我的 Spring java 服务器。

我已经尝试了两种方法。一种是将 JSONArray 作为字符串传递并使用 @PathVariable 注释进行捕获。

这样我可以得到 [{"id":6,"numDishes":1,"observations":"false"},{"id":2,"numDishes":3,"observations":"false"}]我想我可以让它这样工作。

我用于此的代码是:

在安卓中

HttpGet request = new HttpGet(serverURL + action);//action already has two params
HttpResponse response = httpclient.execute(target, request);

在服务器到达 /orderService/addOrder/1/[{"id":6,"numDishes":1,"observations":"false"},{"id":2,"numDishes":3,"observations": “错误的”}]

@RequestMapping(method=RequestMethod.GET, value="/addOrder/{tableNumber}/{jsonParam}")
public void addOrder(@PathVariable Integer tableNumber, @PathVariable String jsonParam) {
    log.info("String encoded: " + jsonParam);
}

无论如何,我宁愿直接做。就像是

安卓

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/>

我在正确的道路上吗?如何将我的对象直接解析为服务器内的自定义对象?

4

1 回答 1

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" />
于 2013-05-12T17:56:34.490 回答