Android HttpURLConnection seems to post nothing? -
any idea why not posting data? php page $_post
array(0){}
or in other words nothing in $_post
. getdata()
string full of data. i'm trying switch deprecated defaulthttpclient
.
httpurlconnection conn = null; try { url u = new url(url); conn = (httpurlconnection) u.openconnection(); conn.setdooutput(true); conn.setdoinput(true); conn.setchunkedstreamingmode(0); conn.setconnecttimeout(10000); conn.setreadtimeout(10000); conn.setrequestmethod("post"); conn.setrequestproperty("content-type", "application/x-www-form-urlencoded"); //conn.setrequestproperty("content-type","application/json"); conn.getoutputstream().write(getdata().getbytes("utf-8")); conn.connect(); inputstream content = new bufferedinputstream(conn.getinputstream()); bufferedreader buffer = new bufferedreader(new inputstreamreader(content)); string s = ""; while ((s = buffer.readline()) != null) { builder.append(s + "\n"); } } catch (exception e) { e.printstacktrace(); } { conn.disconnect(); }
the best approach -->>
class jsonasynctask extends asynctask<string, void, boolean> { protected void onpreexecute() { super.onpreexecute(); dialogoff.setmessage("loading data.."); } protected boolean doinbackground(string... urls) { try { httpget httppost = new httpget(urls[0]); httpclient httpclient = new defaulthttpclient(); httpresponse response = httpclient.execute(httppost); // statusline stat = response.getstatusline(); int status = response.getstatusline().getstatuscode(); if (status == 200) { httpentity entity = response.getentity(); string data = entityutils.tostring(entity); data=new string(data.getbytes("iso-8859-1"), "utf-8"); jsonarray jarray = new jsonarray(data); latlng point; (int = 0; < jarray.length(); i++) jsonobject object = jarray.getjsonobject(i); return true; } } catch (ioexception e) { e.printstacktrace(); } catch (jsonexception e) { e.printstacktrace(); } return false; } protected void onpostexecute(boolean result) { if(result) toast.maketext(getapplicationcontext(), "data transfered successfully", toast.length_long).show(); else{ toast.maketext(getapplicationcontext(), "unable fetch server", toast.length_long).show(); dialogoff.cancel(); } }
}
Comments
Post a Comment