3

I'm a little puzzled by this...

I have a PHP script that is returning the following Json string to an Android application.

{"result":"OK","teams":[{"id":"1","tname":"AAA","tcity":"Paris","tfy":"1901"},{"id":"2","tname":"BBB","tcity":"Barcelona","tfy":"1901"}]}

The question is that for some reason, I can't read the JSonArray that is identified by the "teams" using the following Android code:

@Override
    protected void onPostExecute(String result)
    {
        dialog.dismiss();

        try {
            JSONArray teams = null;
            JSONObject json_response = new JSONObject(result);

            if(json_response.get("result").toString().compareTo("ERR")==0) {
                AlertDialog.Builder adig = new AlertDialog.Builder(ListTeamsActivity.this);
                adig.setMessage(json_response.get("status").toString());
                adig.setCancelable(true);
                AlertDialog diag = adig.create();
                diag.show();
            } else {
                // process the answer
                Log.d("debug_msg", "Teams = " + json_response.get("teams").toString());

                teams = new JSONArray(json_response.getJSONArray("teams"));

                for(int n=0; n< teams.length(); n++) {
                    JSONObject t = teams.getJSONObject(n);
                    arrTeams[n] = t.getString("tname");
                    Log.d("debug_msg", "Team = " + t.getString("tname"));
                }

                ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.activity_list_item, arrTeams);
                lv.setAdapter(adapter);
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

This always results in the same error:

org.json.JSONException: Not a primitive array: class org.json.JSONArray

Here is the full output:

03-25 14:15:19.577: D/debug_msg(1960): Response = {"result":"OK","teams":[{"id":"1","tname":"Sport Lisboa e Benfica","tcity":"Lisboa","tfy":"1901"},{"id":"2","tname":"Sport Lisboa e Benfica","tcity":"Lisboa","tfy":"1901"}]}
03-25 14:15:19.647: D/debug_msg(1960): Teams = [{"tname":"Sport Lisboa e Benfica","id":"1","tfy":"1901","tcity":"Lisboa"},{"tname":"Sport Lisboa e Benfica","id":"2","tfy":"1901","tcity":"Lisboa"}]
03-25 14:15:19.657: W/System.err(1960): org.json.JSONException: Not a primitive array: class org.json.JSONArray
03-25 14:15:19.677: W/System.err(1960):     at org.json.JSONArray.<init>(JSONArray.java:116)
03-25 14:15:19.687: W/System.err(1960):     at pt.iscte.daam.soccerleague.ListTeamsActivity$ListTeams.onPostExecute(ListTeamsActivity.java:113)
03-25 14:15:19.697: W/System.err(1960):     at pt.iscte.daam.soccerleague.ListTeamsActivity$ListTeams.onPostExecute(ListTeamsActivity.java:1)
03-25 14:15:19.697: W/System.err(1960):     at android.os.AsyncTask.finish(AsyncTask.java:632)
03-25 14:15:19.697: W/System.err(1960):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-25 14:15:19.707: W/System.err(1960):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
03-25 14:15:19.707: W/System.err(1960):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-25 14:15:19.707: W/System.err(1960):     at android.os.Looper.loop(Looper.java:137)
03-25 14:15:19.707: W/System.err(1960):     at android.app.ActivityThread.main(ActivityThread.java:4998)
03-25 14:15:19.707: W/System.err(1960):     at java.lang.reflect.Method.invokeNative(Native Method)
03-25 14:15:19.717: W/System.err(1960):     at java.lang.reflect.Method.invoke(Method.java:515)
03-25 14:15:19.717: W/System.err(1960):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-25 14:15:19.717: W/System.err(1960):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-25 14:15:19.727: W/System.err(1960):     at dalvik.system.NativeStart.main(Native Method)

Thank you for your help....

1 Answer 1

7

Why are you creating a new JSON Array? You shouldn't need to create a new one, just get it directly.

teams = json_response.getJSONArray("teams");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this helped! I also had another problem with the string array I was using...

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.