i'm trying send , receive data between android app , server using json unfortunately unable searched lot didn't help. code follows:
in android
try { jsonobject jsonobject = new jsonobject(); jsonobject.put("fromdate", from); jsonobject.put("todate", to); jsonobject.put("reason", reas); new senddata().execute(jsonobject); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); }
and asynctask's doinbackground method
try { httpparams httpparams = new basichttpparams(); httpconnectionparams.setconnectiontimeout(httpparams, cmsinter.timeout); httpconnectionparams.setsotimeout(httpparams, cmsinter.timeout); httpclient client = new defaulthttpclient(httpparams); httppost httppost = new httppost(getresources().getstring( r.string.url)); stringentity se = new stringentity(object.tostring()); se.setcontenttype(new basicheader(http.content_type, "application/json")); httppost.setentity(se); httpresponse response = client.execute(httppost); message = sync.convertresponsetostring(response); } catch (exception e) { e.printstacktrace(); message = e.tostring(); }
and on server side within dopost()
method of servlet code follows :
protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { stringbuffer sb = new stringbuffer(); string line = null; try { bufferedreader reader = request.getreader(); while ((line = reader.readline()) != null) sb.append(line); } catch (exception e) { /* report error */ e.printstacktrace(); } string str = sb.tostring(); try { org.json.jsonobject json = new org.json.jsonobject(str); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } }
and think not right way read jsonobject
on server side because when run org.json.jsonexception: jsonobject text must begin '{' @ character 1
exception , can please me out in this. appreciated. thanks.
finally got line wrong object.tostring()
while sending should have been object[0].tostring()
because asynctask's doinbackground methods parameter array i.e. protected string doinbackground(jsonobject... object)
.
Comments
Post a Comment