How to get json object from android to php -
i trying build web service android app. having problem sending parametres php. using these code blocks http-post.
public string requestjson(string method, jsonobject parameters) { try { int timeout_millisec = 10000; httpparams httpparams = new basichttpparams(); httpconnectionparams.setconnectiontimeout(httpparams, timeout_millisec); httpconnectionparams.setsotimeout(httpparams, timeout_millisec); httpclient client = new defaulthttpclient(httpparams); string tmpurl = serviceurl.trim() + (serviceurl.trim().endswith("/") ? method : "/" + method); httppost request = new httppost(tmpurl); request.setheader(http.content_type, "application/json; charset=utf-8"); if (parameters != null) { request.setentity(new stringentity(parameters.tostring(), http.utf_8)); } httpresponse response = client.execute(request); final int statuscode = response.getstatusline().getstatuscode(); final string jsonresponse = entityutils.tostring(response.getentity()); if (statuscode != httpstatus.sc_ok) { log.w(tag, "error in web request: " + statuscode); log.w(tag, jsonresponse); return null; } else { log.i(tag, jsonresponse); return jsonresponse; } } catch (exception e) { log.e(tag, "error in fetching json due -> " + e.tostring()); } return null; } public string testfunction() { try { jsonobject param = new jsonobject(); param.put("testid", 123); jsoncevabi = requestjson("test.php", param); if (jsoncevabi != null) { jsonobject jo = new jsonobject(jsoncevabi); jsonarray ja = new jsonarray(jo.getstring("d")); @suppresswarnings("unchecked") list<sampleentity> resultlist = (list<sampleentity>) new gson().fromjson(ja.tostring(), new typetoken<list<sampleentity>>() { }.gettype()); return jsoncevabi; } else return null; } catch (exception e) { return null; } }
and php code this;
<?php $id = json_decode($_post['testid']); $json = "{d:[{sample1:'".$id."',sample2:'value12'}]}"; $response = $_get["callback"] . $json; echo $response; ?>
what trying sending testid parametre php. , expenting return me value.
but getting {d:[{sample1:'',sample2:'value12'}]}
what want {d:[{sample1:'123',sample2:'value12'}]}
what missing?
edit: found similar question did not help; android json httpclient send data php server httpresponse
as @chandresh_cool said there problem post paramethers. should edit stringentity this: new stringentity("testid="+parameters.tostring(), http.utf_8)
.
then in server $id variable contains decoded json, should do:
$id = json_decode($_post['testid'], true); $json = "{d:[{sample1:'".$id['testid']."',sample2:'value12'}]}";
Comments
Post a Comment