0

Hi In the below how to get the group name based on json response from the server.how to display in listview. New is the 1st position and Group is the second position

response

{"groupname":"New"}{"groupname":"Group"}

java

 String groupdetails = imService.DispalyGroupDetails(imService.getUsername());
            try {

                JSONArray JA = new JSONArray(groupdetails);
                String[] groupname = new String[JA.length()];  
                for(int i=0;i<JA.length();i++)
                {
                    groupname[i] = JA.getJSONObject(i).getString("groupname");

                }  

            } catch (Exception e) {
               e.printStackTrace();
            }

this is php code Here I am getting the output like this

updated

case "DispalyGroupDetails":
    $userId = authenticateUser($db, $username, $password);

    if ($userId != NULL)

    {

        if (isset($_REQUEST['username']))           
        {               
             $username = $_REQUEST['username'];



             $sql = "select Id from users where username='$username' limit 1";

             if ($result = $db->query($sql))

             {
                    if ($row = $db->fetchObject($result))

                    {    

                                 $sql = "SELECT g.groupname 
                                        FROM `users` u, `friends` f, `group` g 
                                        WHERE u.Id=f.providerId and f.providerId=g.providerId
                                            GROUP BY g.id, g.groupname";
                                $theResult = $db->query($sql);

                                 if ($theResult) {

                    while( $theRow = $db->fetchObject($theResult))
                    { 
                    $json_output[]=$theRow;

                     print(json_encode($json_output));


                    }       
                            //$out = SUCCESSFUL;
                            } else {
                                    //$out = FAILED;
                            }

                    }
                    else
                    {
                        //$out = FAILED;                        
                    }
             }

             else
             {
                    //$out = FAILED;
             }              
        }

        else
        {
                //$out = FAILED;
        }           
    }
    else
    {
        //$out = FAILED;
    }   
break;
tagore
  • 13
  • 6

1 Answers1

0
{"groupname":"New"}{"groupname":"Group"}

First of all your response is not in proper JSON format. the response starts neither with JSONArray nor JSONObject. so firstly find a solution to get the response in proper JSON format on server side.

Possible Option: instead of parsing it with JSON try to do String operations on the response received from the server.

Amrut Bidri
  • 6,276
  • 6
  • 38
  • 80
  • [{"groupname":"New"}][{"groupname":"New"},{"groupname":"Group"}] Now I am getting the response like this – tagore Feb 16 '15 at 09:14
  • even this response that you posted in comments is not a valid JSON response. @tagore – Amrut Bidri Feb 16 '15 at 09:17
  • http://www.thecave.info/output-json-header-in-php/ or http://stackoverflow.com/a/4064468/1576416 this answer might give you some hint on this – Amrut Bidri Feb 16 '15 at 09:24