簡體   English   中英

如何解析來自服務器的android中的多個Json數據

[英]How to parse multiple Json data in android that coming from the server

* 我有疑問,是否有可能從android中的服務器解析多個json響應? 現在我正在開發一個將android應用程序與cakephp網站連接起來的項目。 我從服務器接收數據作為json編碼格式。並解析android中的json數據並顯示到視圖部分。 但現在我想從服務器傳遞多個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();

           }

我想將這三個jso編碼數據傳遞給android

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

當我嘗試只將一個json數據傳遞給android時,它正確得到我的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數據??? 如果有人知道請回復.................

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中編碼3個不同的數組。將它們全部放在一個數組中然后編碼那個單個數組

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



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



  $i++;
}

打印(json_encode($輸出))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM