android - Posting JSON using http , fedup and failed -
i have used around 5 ways post json data http request url isn't getting fruitfull ,its not posting json formatted want , following in json want send , im successfull sending using html:
here's json want: {"x":"n","y":"55"}
here's working html code:
<html><head><script> function sendform(form) { // construct json string in form.value // x string ('cos of quotes) // y integer ('cos of no quotes) form.value.value = "{ \"x\": \"" + form.example1.value + "\", \"y\": " + form.example2.value + " }" form.submit(); } </script></head> <body> <form action="https://api.winv.com/v1/4bhj/306adk" method="post"> <input type="hidden" name="value"> string:<input type="text" name="example1"> number:<input type="text" name="example2"> <input type="button" value="submit" onclick="sendform(this.form);"> </form> </body> </html>
code tried (it's functions)
public void xyz() { try { jsonobject json = new jsonobject(); json.put("x", "nishant"); json.put("y", 34567); httpparams httpparams = new basichttpparams(); httpconnectionparams.setconnectiontimeout(httpparams, 10000); httpconnectionparams.setsotimeout(httpparams, 10000); httpclient client = new defaulthttpclient(httpparams); // //string url = "http://10.0.2.2:8080/sample1/webservice2.php?" + // "json={\"username\":1,\"fullname\":2}"; string url = "url"; httppost request = new httppost(url); request.setentity(new bytearrayentity(json.tostring().getbytes( "utf8"))); request.setheader("json", json.tostring()); httpresponse response = client.execute(request); httpentity entity = response.getentity(); // if response not enclose entity, there no need } catch (throwable t) { toast.maketext(this, "request failed: " + t.tostring(), toast.length_long).show(); } } public void getserverdata() throws jsonexception, clientprotocolexception, ioexception { httpparams httpparams = new basichttpparams(); httpconnectionparams.setconnectiontimeout(httpparams, 10000); httpconnectionparams.setsotimeout(httpparams, 10000); httpclient client = new defaulthttpclient(httpparams); httppost request = new httppost( "url"); // add // // url // here... request.setheader("content-type", "application/json"); jsonobject json = new jsonobject(); json.put("y", "55"); json.put("x", "nishant"); log.i("jason object", json.tostring()); stringentity se = new stringentity(json.tostring()); se.setcontentencoding("utf-8"); se.setcontenttype("application/json"); request.setentity(se); httpresponse response = client.execute(request); httpentity entity = response.getentity(); inputstream = entity.getcontent(); string _response = convertstreamtostring(is); system.out.println("res-- " + _response); // check if server response valid code int res_code = response.getstatusline().getstatuscode(); system.out.println("code-- " + res_code); } private static string convertstreamtostring(inputstream is) { bufferedreader reader = new bufferedreader(new inputstreamreader(is), 8192); stringbuilder sb = new stringbuilder(); string line = null; try { while ((line = reader.readline()) != null) { sb.append((line + "\n")); } } catch (ioexception e) { e.printstacktrace(); } { try { is.close(); } catch (ioexception e) { e.printstacktrace(); } } return sb.tostring(); } public void postdata() { // create new httpclient , post header httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost( "url"); try { // add data list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("", "nishant")); namevaluepairs.add(new basicnamevaluepair("x:", "45")); httppost.setentity(new urlencodedformentity(namevaluepairs)); // execute http post request httpresponse response = httpclient.execute(httppost); } catch (clientprotocolexception e) { // todo auto-generated catch block } catch (ioexception e) { // todo auto-generated catch block } } protected void sendjson(final string email, final int pwd) { thread t = new thread() { public void run() { looper.prepare(); // preparing message pool child // thread httpclient client = new defaulthttpclient(); httpconnectionparams.setconnectiontimeout(client.getparams(), 10000); // timeout limit httpresponse response; jsonobject json = new jsonobject(); try { httppost post = new httppost( "url"); json.put("x", email); json.put("y", pwd); stringentity se = new stringentity(json.tostring()); se.setcontenttype(new basicheader(http.content_type, "application/json")); post.setentity(se); response = client.execute(post); /* checking response */ if (response != null) { inputstream in = response.getentity().getcontent(); // // // data // in // // entity } } catch (exception e) { e.printstacktrace(); // createdialog("error", "cannot estabilish connection"); } looper.loop(); // loop in message queue } };
please tell me why not posting? wrong done?
looks lot of code. think better organised encapsulating json object in separate method, perhaps use example below, works me. seem have lot of http connection objects don't think necessary...
public static boolean sendjsontoblx(string path, jsonobject json) throws exception { defaulthttpclient httpclient = new defaulthttpclient(); httppost httpost = new httppost(path); stringentity se = new stringentity(json.tostring()); httpost.setentity(se); httpost.setheader("accept", "application/json"); httpost.setheader("content-type", "application/json"); @suppresswarnings("rawtypes") responsehandler responsehandler = new basicresponsehandler(); @suppresswarnings("unchecked") string response = httpclient.execute(httpost, responsehandler); jsonobject jsonresponse = new jsonobject(response); string serverresponse = jsonresponse.getstring("success"); if (serverresponse.equals("true")) { return true; } else { return false; } }
i creating json object with:
public final static jsonobject writejson() throws exception { jsonobject obj = new jsonobject(); try { obj.put("x", "nishant"); obj.put("y", 34567); } catch (jsonexception e) { e.printstacktrace(); } return obj; }
new info: after testing locally, appears it's ssl problem android not accessing https. nothing json etc. above answer works fine plain http.
Comments
Post a Comment