RestTemplate for Android is not properly converting json to http request for postForObjects method -
i trying post json object .net web service:
resttemplate resttemplate = new resttemplate(); resttemplate.getmessageconverters().add(new gsonhttpmessageconverter()); answer[] answers = resttemplate.postforobject(url, new gson().tojson(request), answer[].class); the generated json looks fine far:
{"request":1234} but when sent web service of resttemplate content of http request kind of messed up:
"{\"request\":1234}" and service responds error code 400 bad request
edit: found problem
the problem encoded object twice. resttemplate encodes object json.
the working code is:
resttemplate resttemplate = new resttemplate(); resttemplate.getmessageconverters().add(new gsonhttpmessageconverter()); answer[] answers = resttemplate.postforobject(url, request, answer[].class);
use method post json on server public string postdata(string url, jsonobject obj) { // create new httpclient , post header string inserttransactionresult = null; httpclient httpclient = new defaulthttpclient(); httpparams myparams = new basichttpparams(); httpconnectionparams.setconnectiontimeout(myparams, 1000); httpconnectionparams.setsotimeout(myparams, 1000); try { httppost httppost = new httppost(url.tostring()); httppost.setheader("content-type", "application/json"); stringentity se = new stringentity(obj.tostring()); se.setcontentencoding(new basicheader(http.content_type, "application/json")); httppost.setentity(se); httpresponse response = httpclient.execute(httppost); result = entityutils .tostring(response.getentity()); } catch (clientprotocolexception e) { } catch (ioexception e) { } return result; }
Comments
Post a Comment