JSON array returned to Android is not having complete data

I'm working on a PHP, Laravel and Android application.

Laravel provides all the JSON data from server (a website is also required) and Android app is the consumer.

I'm trying to read all groups of a user with the following function:

public function dataAllGroups($id)
{
    if(isset($id)){

        $user = User::find($id);

        if(isset($user)){
            $groups = UserGroup::where('status', '=', 'active')->where('user_id', $id)->get();

            $queries = DB::getQueryLog();
            $last_query = end($queries);

            if(isset($groups) && count($groups)>0)
                return array('message' => 'found', 'groups' => $groups, 'count' => count($groups), 'query' => $last_query);
            else
                return array('message' => 'empty', 'query' => $last_query);
        }
        else
            return array('message' => 'invalid');
    }
    else
        return array();
}

This returns following in logcat

05-11 09:04:14.646    2531-2554/com.activetasks.activetasks D/group read resultīš• {"message":"found","groups":{},"count":2,"query":{"query":"select * from `user_groups` where `status` = ? and `user_id` = ?","bindings":["active","14"],"time":1}}

So, the query is correct, count says I'm getting 2 records but groups says {}

This is a bit weird for me.

This is my Android logic for reading JSON array:

protected String doInBackground(String... params) {

    StringBuilder builder = new StringBuilder();

    String result = null;

    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet get = new HttpGet(url);

        try{

            HttpResponse response = httpClient.execute(get);
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }

            result = builder.toString();
        }
        catch (Exception e) {
            e.printStackTrace();
        }


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

    return result;
}

And here I read the returned groups:

                JSONArray jArray = json.getJSONArray("groups");

                for(int i=0; i<jArray.length(); i++){
                    JSONObject json_data = jArray.getJSONObject(i);

                    Group group = new Group();

                    group.setId(json_data.getInt("id"));
                    group.setName(json_data.getString("name"));

                    groups.add(group);
                }

                groupAdapter.notifyDataSetChanged();

Answers


groups is your JSONObject not a JSONArray.

You need to change from

 JSONArray jArray = json.getJSONArray("groups");

to

 JSONObject Jb= json.getJSONObject("groups");

I found the solution. I changed the code to:

'groups' => $groups->toArray()

and I'm getting groups data.


Need Your Help

Releasing WinForm Program Updates

c# winforms windows-installer release

I'd like to release some updates for a WinForm program, but to date I have simply released an all-new compile. People have to un-install the old version and install the new version.

How to disable the back button in browser when user logout in asp.net c#

c# asp.net

Our problem is we are able to clear session on logout.