-1

I can connect to server and get json data. it is in this format:

    {"clazz":"ManiaContestantList","contestants":[

    {"clazz":"ManiaContestant","contestantId":"1","contestantName":"Adira","photoUrl":"/fileFeed.action?service=astroManiaService&action=viewFile&type=JPG&path=1/1_CONTESTANT_PHOTO.png"},
    {"clazz":"ManiaContestant","contestantId":"2","contestantName":"Akim","photoUrl":"/fileFeed.action?service=astroManiaService&action=viewFile&type=JPG&path=2/2_CONTESTANT_PHOTO.png"},
.
.
.
    ]}

My code is like this:

@Override
    protected void onStart() {
        super.onStart();
        Log.i("NewsList", "inside onStart();");

        try {
            URL url = new URL("/MY URL/");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(Manager.ConnTimeout);
            conn.setReadTimeout(Manager.ReadTimeout);

            int responseCode = conn.getResponseCode();
            Log.i("Connection oppened", "Response code is:" + responseCode);
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                if (in != null) {
                    StringBuilder strBuilder = new StringBuilder();
                    // Read character by character              
                    int ch = 0;
                    while ((ch = in.read()) != -1)
                        strBuilder.append((char) ch);

                    // get returned message and show it
                    strServerResponseMsg = strBuilder.toString();
                    Log.i("Data returned by server:", strServerResponseMsg);



                    JSONArray jObjects = new JSONArray(strServerResponseMsg);
                    for(int i=0; i<jObjects.length(); i++){
                        System.out.println(jObjects.getJSONObject(i).getString("contestantId").toString());
                        System.out.println(jObjects.getJSONObject(i).getString("contestantName").toString());
                        System.out.println(jObjects.getJSONObject(i).getString("photoUrl").toString());
                }
                in.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }       


    }

I have my responsed Json in "strServerResponseMsg" variable. but in line "JSONArray jObjects = new JSONArray(strServerResponseMsg);" Logcat shows typemismatch error.

Please tell me what is my problem?

Hesam
  • 52,260
  • 74
  • 224
  • 365

4 Answers4

4

{ implies it is an Object. [ implies it is an Array. As your string starts with { so it is an JSONObject rather than JSONArray.

JSONArray jObjects = new JSONArray(strServerResponseMsg);

must be like this

JSONObject jObjects = new JSONObject(strServerResponseMsg);
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
  • Thanks. I did it and changed array as: JSONArray contestantObjects = new JSONArray((Collection) jObject.getJSONArray("contestants")); but after running crash happened. Do you have any idea? – Hesam Apr 05 '12 at 05:28
  • Try JSONArray contestantObjects= jObject.getJSONArray("contestants"); for access refer http://stackoverflow.com/questions/1568762/jsonarray-with-java – Mohammed Azharuddin Shaikh Apr 05 '12 at 05:31
1

The json example given is not a jsonarray it is a jsonobject so you should so something like the following

JSONObject jObj = new JSONObject(strServerResponseMsg);
JSONArray jArray = new JSONArray(jObj.getJSONArray("contestants"));

and then iterate through jArray

Josnidhin
  • 12,469
  • 9
  • 42
  • 61
0

That is a JSONObject you are getting as is evident by the '{'.

  JSONObject j=null;

    try{
 j=new JSONObject(strServerResponseMsg);
}
catch(JSONException r)
{
    //Handle it.
    r.printStackTrace();


 }
 Log.w("json",j.toString());
Sankalp
  • 1,128
  • 4
  • 14
  • 31
0

JSON is backdated, if you use json you have to be very careful to handle. So that, use GSON api developed by google. It is made by help of json api. Please visit this link- Click here

Suvam Roy
  • 1,282
  • 2
  • 11
  • 21