2013-05-08 66 views
0

我想從Android傳遞一個JSONArray到我的Spring java服務器。從Android到傑克遜的自定義對象的Spring JSON映射

我已經試過兩種方法。一種是將JSONArray作爲字符串傳遞並使用@PathVariable註釋捕獲。

這樣我就可以得到[{「id」:6,「numDishes」:1,「observations」:「false」},{「id」:2,「numDishes」:3,假「}],我想我可以使它這樣工作。

我用於此的代碼是:

在機器人

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

在服務器到達/ orderService/addOrder/1/[{ 「ID」:6, 「numDishes」:1,」觀察 「:」 假 「},{」 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); 
    } 

而且,因爲我認爲這可能是問題的一部分,我有這個我春天的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

爲了記錄:我沒有收到任何錯誤,或者消息 – inor 2013-05-09 18:46:50

回答

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