2014-03-27 16 views
0

我正在开发一个android应用程序,我使用php/doctrine从数据库中检索数据。我把这些数据放在一个json对象中。使用json在php和android之间进行链接

问题是我不知道如何在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); 
} 
+0

查看我已经回答的问题[这里] [1]你可以通过这个例子来实现。 [1]:http://stackoverflow.com/a/22558535/3360307 –

+0

http://www.androidhive.info/2012/05/how-to-connect-android-with-php- mysql/ –

+0

基本上这个教程是你想要的。保存你一个点击http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ –

回答

1

请遵循这一简单的教程here

0

你必须首先获得服务器的请求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; 
相关问题