I am working on an android app and I am using php/doctrine to retrive the data from the database. I put said data in a json object.
The problem is that I do not know how to display it on the android.
This is the php code:
function getTransaction($id) {
$transaction = $this->getDoctrine()
->getRepository('nameBundle:Transaction')
->find($id);
if(!$transaction) {
throw $this->createNotFoundException('No transaction found'.$id);
}
$array = $transaction->toArray();
$json = json_encode($array);
}
Please follow this simple tutorial here
- You have to deploy your php code in server.
- Assume you have url (http://xxxx.com/getJson.php) which return json
- Create HttpClient and make a call.
- get json data and parse the response and display.
More details Check this Read Json From url
You have to get first the request httpPost of the server.
protected Boolean doInBackground(String... params){
boolean resul = true;
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new
HttpPost("http://example/getinfo.php");
post.setHeader("content-type", "application/json");}
And then parse the json object.
try{
//Build the json object
JSONObject dataJson = new JSONObject();
dataJson.put("lastName", params[0]);
dataJson.put("phoneNumer", Integer.parseInt(params[1]));
StringEntity entity = new StringEntity(dataJson.toString());
post.setEntity(entity);
HttpResponse resp = httpClient.execute(post);
String respStr = EntityUtils.toString(resp.getEntity());
if(!respStr.equals("true"))
resul = false;
}
catch(Exception ex)
{
Log.e("Rest","Error", ex);
resul = false;
}
return resul;