1

Here's what im trying to do. There are two arrays that i want to retrieve from my mysql database. These two arrays are echoed separately:

echo json_encode(array("friendrequest"=>$friendrequest));
echo json_encode(array("friendresult"=>$friendresult));

This is the code i use to process the resulting string:

public void onResponse(String response) {
        Log.d("Response", response);
        try {

            JSONObject jsonObj = new JSONObject(response);

            JSONArray ja_data = jsonObj.getJSONArray("friendresult");
            String[] from = {
                "first_name",
                "anytimer_count",
                "relationship_status"
            }; //string array
            int[] to = {
                R.id.textListView1,
                R.id.textListView2,
                R.id.textListView3
            }; //int array of views id's
            JSONArrayAdapter arrayListAdapter = new JSONArrayAdapter(MyFriendsActivity.this, ja_data, R.layout.custom_list_items, from, to);
            list.setAdapter(arrayListAdapter);


            JSONArray ja_requests = jsonObj.getJSONArray("friendrequest");
            int ja_lengths = ja_requests.length();
            Log.d("Response", Integer.toString(ja_lengths));

        } catch (JSONException e) {
            Log.e("MyFriendsActivity", "unexpected JSON exception", e);
        }

Now, the logged reponse from line 2 in the processing code gives me the following:

Response =

{
  "friendrequest": [
    {
      "first_name": "Tom",
      "anytimer_count": "0",
      "relationship_status": "0"
    },
    {
      "first_name": "Bert",
      "anytimer_count": "0",
      "relationship_status": "0"
    }
  ]
}{
  "friendresult": [
    {
      "first_name": "Luuk",
      "anytimer_count": "0",
      "relationship_status": "1"
    }
  ]
}

This is contains all the values that should be sent and is correct.

The problem is that my processing code is only able to recognize the array that is encoded first in the php script. Basically if i swap the position of the two lines of code in the php script, 'friendrequest' will contain values, while 'friendresult' does not and vice versa. What am i doing wrong here?

The error im getting:

org.json.JSONException: No value for friendresult

at com.example.tomva.anytimereverywhere.MyFriendsActivity$3.onResponse(MyFriendsActivity.java:84)

Line 84 is the following line:

JSONArray ja_data = jsonObj.getJSONArray("friendresult");
Bhavik Patel
  • 1,044
  • 1
  • 15
  • 33
  • 2
    if this is the full response on your request, it is not a valid json. missing enclosing `[ ]` and a `,` between your two objects. than your request is giving you an JsonArray with 2 Objects,..... – Fusselchen Aug 18 '17 at 12:53

3 Answers3

3

You have to pass yours json in a array to be able to extract them all.

Shold be:

[
<?php
   echo json_encode(array("friendrequest"=>$friendrequest));
   echo ",";
   echo json_encode(array("friendresult"=>$friendresult));
?>
]

Related answer

or you can use following

echo json_encode(array("friendrequest"=>$friendrequest,"friendresult"=>$friendresult));
sam chaudhari
  • 756
  • 1
  • 12
  • 24
  • yes, that will produce a valid JsonArray. a better way ist the answer from @sam-chaudhari . Now you have to change your code from parsing into an JsonObject t JsonArray, with 2 object and then you can use your code. – Fusselchen Aug 18 '17 at 13:13
1

Replace

echo json_encode(array("friendrequest"=>$friendrequest));
echo json_encode(array("friendresult"=>$friendresult));

With this

echo json_encode(array("friendrequest"=>$friendrequest,"friendresult"=>$friendresult));
sam chaudhari
  • 756
  • 1
  • 12
  • 24
0

Your Response JSON is not valid as it has missing "," try to change your response from

{"friendrequest": [{"first_name":"Tom","anytimer_count":"0","relationship_status":"0"},{"first_name":"Bert","anytimer_count":"0","relationship_status":"0"}]} {"friendresult": [{"first_name":"Luuk","anytimer_count":"0","relationship_status":"1"}]}

To

[{
    "friendrequest": [{
        "first_name": "Tom",
        "anytimer_count": "0",
        "relationship_status": "0"
    }, {
        "first_name": "Bert",
        "anytimer_count": "0",
        "relationship_status": "0"
    }]
}, {
    "friendresult": [{
        "first_name": "Luuk",
        "anytimer_count": "0",
        "relationship_status": "1"
    }]
}]

Common Errors

  • Expecting 'STRING' - You probably have an extra comma at the end of
    your collection. Something like { "a": "b", }
  • Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[' - You probably have an extra comma at the end of your list. Something like: ["a", "b", ]

  • Enclosing your collection keys in quotes. Proper format for a
    collection is { "key": "value" }

  • Make sure you follow JSON's syntax properly. For example, always use double quotes, always quotify your keys, and remove all callback functions
Shailesh Bandil
  • 497
  • 7
  • 20