1

I know this is a very commonly asked question and I found quite a lotta posts in SO. My code is as below and I don't see a reason why the json array is returning null. Could someone please review it for me please? I've left no stones left unturned! :(

JsonArray jsonArr = JSONfunctions.getJSONfromURL(url);
    try {
        if (jsonArr != null) {
            for (int i = 0; i < jsonArr.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject jsonObj = jsonArr.getJSONObject(i);

                map.put(TAG_CODE, jsonObj.getString("Code"));
                map.put(TAG_DISPLAY_NAME, jsonObj.getString("UserName"));
                map.put(TAG_PROGRAM_NAME, jsonObj.getString("Password"));
                mylist.add(map);
            }
        }
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }                                                                                      






public class JSONfunctions {

public static JSONArray getJSONfromURL(String url) {
    InputStream is = null;
    String result = "";
    JSONArray jArray = null;

    // http post
    try {

        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httppost = new HttpGet(url);
        // httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }

    // convert response to string
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();

                // Result is always returning me an array.(not null)

    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + result);
    }
    try {
        jArray = new JSONArray(result);

                // Goes into the catch block

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        Log.e("log_tag", "Error parsing result " + result);
    }
    return jArray;

}

}

9
  • Where do you kept this code, are using separate thread or AsyncTask? Commented May 11, 2013 at 11:52
  • In a separate class file but not in Aysnc Task. It seem to be working sometime back. :( Commented May 11, 2013 at 11:53
  • It will work on older android version (pre 3.0). You must move it in an AsyncTask Commented May 11, 2013 at 11:54
  • 1
    What is the error message? I mean when it goes to the catch block. Commented May 11, 2013 at 11:55
  • 1
    what the server is returning is not a JSONArray but a JSONOBject. you should change the type of jArray from JSONArray to JSONObject Commented May 11, 2013 at 12:15

1 Answer 1

1

What the server is returning ( {"FirstName": "\"firstname"", "LastName": "\"lastname\"", "Code": "\"id\""} ) is a JSONObject and not a JSONArray, you should change

JSONArray jArray = null;

in

JSONOBject jArray = null;

and

jArray = new JSONArray(result);

in

jArray = new JSONOBject (result);
Sign up to request clarification or add additional context in comments.

4 Comments

JSONObject json = JSONfunctions.getJSONfromURL(url); JSONArray jsonArr= json.getJSONArray(TAG); What do you suggest to handle the unnamed arrays where the tag is absent? Also it's a JsonArray cuz the the tags are absent. Hence I can use getJsonFromUrl directly.
you do not have a jsonarray. You have a jsonobject.
Alright! So the next question is how do I handle the JSONobject in case of unnamed arrays? This is the only way I could find to do it. And it has worked before. Do you have a sample to handle unnamed arrays using JSONObject?
I tried without returning the JSONobject and pushed the values into a hashmap immediately after I get the object and it worked.

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.