java - Passing Parameters with HttpURLConnection -
with old apache stuff deprecated in api 22, getting around updating network stuff.
using openconnection() seems pretty straight forward. however, have not seen example send parameters it.
how update code?
arraylist<namevaluepair> param = new arraylist<namevaluepair>(); param.add(new basicnamevaluepair("user_id",string.valueof(userid))); httppost.setentity(new urlencodedformentity(param)); now namevaluepair , basicnamevaluepair deprecated.
edit: server side php code doesn't expect json parameters , can't of sudden switch due existing users -- non-json answer recommended.
edit2: need target android 4.1+ @ moment.
i fixed this:
httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setdooutput(true); conn.setrequestmethod("post"); conn.setrequestproperty("content-type", "application/x-www-form-urlencoded"); here parameter stuff:
string charset = "utf-8"; string s = "unit_type=" + urlencoder.encode(mainactivity.distance_units, charset); s += "&long=" + urlencoder.encode(string.valueof(mainactivity.mlongitude), charset); s += "&lat=" + urlencoder.encode(string.valueof(mainactivity.mlatitude), charset); s += "&user_id=" + urlencoder.encode(string.valueof(myndquest.userid), charset); conn.setfixedlengthstreamingmode(s.getbytes().length); printwriter out = new printwriter(conn.getoutputstream()); out.print(s); out.close();
Comments
Post a Comment