java - JSONObject sent to .net service via HttpPost is recieving null JSONObject -
i need consume json webservice method accepts stringified jsonobject parameter,
this how making jsonobject
btnregister.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub jsonobjsend = new jsonobject(); try{ jsonobjsend.put("firstname", firstname.gettext().tostring()); jsonobjsend.put("lastname", lastname.gettext().tostring()); jsonobjsend.put("emaillid", emailid.gettext().tostring()); jsonobjsend.put("password", password.gettext().tostring()); jsonobjsend.put("mobilenumber", mobilenumber.gettext().tostring()); // jsonobjsend.put("login_rememberme", "true"); // jsonobject header = new jsonobject(); // header.put("devicetype","android"); // device type // header.put("deviceversion","2.0"); // device os version // header.put("language", "es-es"); // language of android client // jsonobjsend.put("header", header); }catch (jsonexception e) { e.printstacktrace(); } new sendjsonobect().execute(url); } }); }
public class sendjsonobect extends asynctask {
@override protected string doinbackground(string... params) { // todo auto-generated method stub string jsonobjrecv = httpclient.sendhttppost(params[0],jsonobjsend); log.d("jsonobjsend",""+jsonobjsend); log.d("jsonobjrecv",""+jsonobjrecv); return jsonobjrecv.tostring(); } @override protected void onpostexecute(string result) { // todo auto-generated method stub super.onpostexecute(result); toast.maketext(registration.this, result,toast.length_long ).show(); log.d("result", ""+result); }
}
this method making request call
public static string sendhttppost(string url, jsonobject jsondata) { try { defaulthttpclient httpclient = new defaulthttpclient(); httppost httppostrequest = new httppost(url); stringentity se=null; try { se = new stringentity(jsondata.tostring(),http.utf_8); } catch (unsupportedencodingexception e) { // todo auto-generated catch block e.printstacktrace(); } se.setcontenttype("application/json"); se.setcontentencoding("utf-8"); // set http parameters httppostrequest.setentity(se); log.d("test", ""+se); // httppostrequest.setheader("accept", "application/json"); // httppostrequest.setheader("content-type", "application/json"); long t = system.currenttimemillis(); httpresponse response = (httpresponse) httpclient.execute(httppostrequest); log.i(tag, "httpresponse received in [" + (system.currenttimemillis()-t) + "ms]"); // hold of response entity (-> data): httpentity entity = response.getentity(); if (entity != null) { // read content stream inputstream instream = entity.getcontent(); header contentencoding = response.getfirstheader("content-encoding"); if (contentencoding != null && contentencoding.getvalue().equalsignorecase("gzip")) { instream = new gzipinputstream(instream); } // convert content stream string string resultstring= convertstreamtostring(instream); instream.close(); resultstring = resultstring.substring(1,resultstring.length()-1); // remove wrapping "[" , "]" // transform string jsonobject // jsonobject jsonobjrecv = new jsonobject(resultstring); //// raw debug output of our received json object: // log.i(tag,"<jsonobject>\n"+jsonobjrecv.tostring()+"\n</jsonobject>"); return resultstring; } } catch (exception e) { // more http exception handling in tutorial. // print stack trace. e.printstacktrace(); } return null; }
the jsonobject received @ service side showing null.
what mistake doing?.
did try application/x-www-form-urlencoded content type.
httppostrequest.addheader("content-type", "application/x-www-form-urlencoded");
Comments
Post a Comment