【问题标题】:Linking between php and android using json使用 json 在 php 和 android 之间链接
【发布时间】:2014-05-06 08:40:14
【问题描述】:

我正在开发一个 android 应用程序,我正在使用 php/doctrine 从数据库中检索数据。我把所说的数据放在一个 json 对象中。

问题是我不知道如何在 android 上显示它。 这是php代码:

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

【问题讨论】:

标签: php android json api doctrine


【解决方案1】:

请按照这个简单的教程here

【讨论】:

    【解决方案2】:
    1. 您必须在服务器中部署您的 php 代码。
    2. 假设你有返回 json 的 url (http://xxxx.com/getJson.php)
    3. 创建 HttpClient 并进行调用。
    4. 获取json数据并解析响应并显示。

    更多详情请查看Read Json From url

    【讨论】:

      【解决方案3】:

      你要先得到服务器的请求httpPost。

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

      然后解析json对象。

      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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-03
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 1970-01-01
        • 2015-12-12
        • 2019-04-04
        相关资源
        最近更新 更多