简体   繁体   中英

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

* I'm having a doubt , is it possible to parse multiple json response from the server in android ? now i am working on a project that connect an android application with a cakephp website. i receive data from the server as json encoded format.and parse that json data in android and display to the view part. but now i want to pass multiple json response from the server , How it possible ??? *

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

           }

I want to pass these three jso encoded data to the android

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

when i try to pass only one json data to the android, it is getting correctly my android code is

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

                  }

What i need to add in my android code to receive multiple json data??? IF anybody knows please reply.................

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

And on php side you are encoding 3 different arrays in json.Put them all together in one array and then encode that single array

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



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



  $i++;
}

print(json_encode($output))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM