Linking between php and android using json

2019-08-30 07:07发布

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);
}

3条回答
该账号已被封号
2楼-- · 2019-08-30 07:07

Please follow this simple tutorial here

查看更多
地球回转人心会变
3楼-- · 2019-08-30 07:22

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;
查看更多
forever°为你锁心
4楼-- · 2019-08-30 07:32
  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

查看更多
登录 后发表回答