2013-02-07 49 views
0

* 我有疑问,是否有可能从android中的服务器解析多个json响应?现在我正在开发一个连接android应用程序和cakephp网站的项目。我从服务器接收数据作为json编码格式。并在android中解析该json数据并显示到视图部分。 但现在我想从服务器传递多个json响应,怎么可能? *如何解析来自服务器的Android中的多个Json数据

function commuterJson() 
    { 
    $upid=$_POST['upid']; 
    $ampm=$_POST['ampm']; 
     $this->loadModel('Userprofile'); 
    $this->Userprofile->recursive = -1; 
    $ups = $this->Userprofile->find('first', array('conditions' => 
    array('id' => $upid, 'status' => 'active'))); 
    $todaysdata = $this->Requestcard->getRequestcardDataampm($upid, $ampm, $today); 

    $driverId=$todaysdata[0]['Requestcard']['driver_id']; 
    $vacencyId=$todaysdata[0]['Requestcard']['vacancycard_id']; 
    $driverDetails = $this->Userprofile->find('first', array('conditions' => 
    array('id' => $driverId, 'status' => 'active'))); 
    $vacancyDetails = $this->Vacancycard->find('first', 
    array('conditions' => array('id' =>$vacencyId))); 
    $vechicleId=$vacancyDetails['Vacancycard']['vehicledetail_id']; 
    $vechicleDetails=$this->Vehicledetail->find('first', 
    array('conditions' => array('id' => $vechicleId))); 

    echo json_encode($driverDetails); 
    echo json_encode($vechicleDetails); 
    echo json_encode($todaysdata); 
    exit(); 

      } 

我想,当我尝试只有一个JSON数据传递到Android这三个JSO编码的数据传递到Android

echo json_encode($driverDetails); 
    echo json_encode($vechicleDetails); 
    echo json_encode($todaysdata); 

,它是正确越来越 我的Android代码是

   public void getData(View v) 
        { 
       HttpClient client = new DefaultHttpClient(); 
       HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); 
       HttpResponse response; 

     try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = 
         new HttpPost("http://10.0.2.2/Mebuddie/logins/login1"); 

       httppost.setEntity 
          (new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8)); 
       response = httpclient.execute(httppost); 
       StringBuilder builder = new StringBuilder(); 
      BufferedReader reader = new BufferedReader 
      (new InputStreamReader(response.getEntity().getContent(), "UTF-8")); 
       for (String line = null; (line = reader.readLine()) != null;) 
         { 
        builder.append(line).append("\n"); 
         } 
      JSONTokener tokener = new JSONTokener(builder.toString()); 
      JSONArray finalResult = new JSONArray(tokener); 
      Object type = new Object(); 
     if (finalResult.length() == 0 && type.equals("both")) 
      { 
      System.out.println("null value in the json array"); 

        } 
     else { 
       JSONObject json_data = new JSONObject(); 
         for (int i = 0; i < finalResult.length(); i++) 
         { 
        json_data = finalResult.getJSONObject(i); 
        JSONObject menuObject = json_data.getJSONObject("Userprofile"); 

          group_id= menuObject.getString("group_id"); 
          id = menuObject.getString("id"); 


        } 
     catch (Exception e) { 
       Toast.makeText(FirstMain.this, 
       "please enter a valid id or pswd",Toast.LENGTH_LONG).show(); 
       e.printStackTrace(); 
        } 

        } 

我需要在我的android代码中添加接收多个json数据? 如果有人知道请回复.................

+0

只是觉得......使它象:'{ “driverDetails”:$ driverDetails, “vechicleDetails”:$ vechicleDetails, “todaysdata”:$ todaysdata}'或者像这样不喜欢 – Selvin

+0

嗨Selvin ...那是一个很有创意的想法..... Iam试图这样想.. –

回答

1
public void getData(View v) 
{ 
    // TODO Auto-generated method stub 
    try{ 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://yourpagename"); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 
     }catch(Exception e){ 
     Log.e("log_tag", "Error in http connection"+e.toString()); 
    } 
    try{ 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     result=sb.toString(); 
     }catch(Exception e){ 
     Log.e("log_tag", "Error converting result"+e.toString()); 
    } 
    return null; 
} 
protected void onPostExecute(Void v) { 
    try{ 
     JSONArray jArray = new JSONArray(result); 
     for(int i=0;i<jArray.length();i++) 
     { 
      JSONObject json_data = jArray.getJSONObject(i); 
      String id=json_data.getString("id"); 
      String name=json_data.getString("name"); 
     } 
    } 
    catch(JSONException e){ 
     Log.e("log_tag", "Error parsing data "+e.toString()); 
    } 
} 

而在PHP端你在json.Put他们都聚集在一个编码3个不同的排列数组,然后编码单个阵列

+0

这是什么?没有答案......你不明白他的问题 – Selvin

+0

这是一个答案,我告诉他使用这个简单的代码,而不是他的困难的代码,我认为你没有看到我的最后一行看到它睁开眼睛“并在PHP一侧你在json中编码3个不同的数组,然后编码这个单个数组“这是答案 –

+0

我得到了解决方案,在我的php代码中,我将这些数据赋予了单个json arry $ combain =阵列($ vechicleDetails + $ driverDetails + $ todaysdata); ..........................感谢萨马尔先生,并再次感谢塞尔文......感谢您的大力支持 –

1
while($row=mysql_fetch_assoc($sql)) 
{ 
    $output[$i]['id'] = $row['$driverDetails']; 
    $output[$i]['name'] = $row['$vechicleDetails']; 



    $output[$i]['image']=$row['$todaysdata']; 



    $i++; 
} 

打印(json_encode($输出))

相关问题