1

*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.................

2
  • just think ... make it like: { "driverDetails" : $driverDetails, "vechicleDetails": $vechicleDetails, "todaysdata" : $todaysdata} or smth like this Commented Feb 7, 2013 at 11:48
  • Hi Selvin...thats a wounderful idea.....Iam trying to think like that.. Commented Feb 7, 2013 at 11:54

2 Answers 2

2
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

Sign up to request clarification or add additional context in comments.

4 Comments

what is this? not an answer ... you do not understand his problem
this is an answer i have told him to use this simple code instead of his difficult code and i think you haven seen my last line see it with open eyes"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"this is the answer
I got the solution ,in my php code i combain those data that gives a single json arry $combain=array($vechicleDetails+$driverDetails+$todaysdata); ..........................Thanks Saammar javed, and once again Thanks selvin...thanks for your great support
if we have solved your problem you should vote for us :-) thats the good way to say thanx
1
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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.