0

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);
}
Andresch Serj
  • 35,217
  • 15
  • 59
  • 101
user3100088
  • 335
  • 3
  • 8
  • 20

3 Answers3

1

Please follow this simple tutorial here

Himanshu Agarwal
  • 4,623
  • 5
  • 35
  • 49
0
  1. You have to deploy your php code in server.
  2. Assume you have url (http://xxxx.com/getJson.php) which return json
  3. Create HttpClient and make a call.
  4. get json data and parse the response and display.

More details Check this Read Json From url

Prakash M
  • 651
  • 3
  • 13
0

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;
Sergi Case
  • 226
  • 2
  • 12