Spring JSON Mapping from Android to Custom Object with jackson -
i'm trying pass jsonarray android spring java server.
i've tried 2 ways. 1 passing jsonarray string , catching @pathvariable annotation.
this way can [{"id":6,"numdishes":1,"observations":"false"},{"id":2,"numdishes":3,"observations":"false"}] , guess make work way.
the code use is:
in android
httpget request = new httpget(serverurl + action);//action has 2 params httpresponse response = httpclient.execute(target, request);
in server arrives /orderservice/addorder/1/[{"id":6,"numdishes":1,"observations":"false"},{"id":2,"numdishes":3,"observations":"false"}]
@requestmapping(method=requestmethod.get, value="/addorder/{tablenumber}/{jsonparam}") public void addorder(@pathvariable integer tablenumber, @pathvariable string jsonparam) { log.info("string encoded: " + jsonparam); }
anyway i'd prefer directly. like
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);
server
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); }
also, because think part of problem, have inside 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/>
am in right path? how can parse object directly custom object inside server?
here solution
//android client. calling 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 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" />
Comments
Post a Comment