0

I have a problem with parsing JsonArray response.

I take JsonObject from JsonArray, parse it and set in entity message and then that message add to ArrayList.

Problem is that in ArrayList that I want to return I always have only one message. This must be some fundamental error but I cant find it.

public ArrayList<Message> getSearchInfo(String response) {
    ArrayList<Message> searchResult = new ArrayList<Message>();

    int jsonMessageId = -1;
    String jsonDate = "";
    String jsonText = "";
    String jsonAutor = "";
    String jsonSource = "";
    int jsonThemeID = -1;
    int jsonSourceID = -1;

    try {
        JSONArray jArray = new JSONArray(response);

        if (jArray != null) {

            for (int i = 0; i < jArray.length(); i++) {
                try {

                    JSONObject oneObject = jArray.getJSONObject(i);
                    Message m = new Message();
                    // Pulling items from the array

                    jsonMessageId = oneObject.getInt("MessageId");
                    jsonDate = oneObject.getString("CreatedDate");
                    jsonText = oneObject.getString("TextMessage");
                    jsonAutor = oneObject.getString("Autor");
                    jsonSource = oneObject.getString("Source");
                    jsonThemeID = oneObject.getInt("ThemeId");
                    jsonSourceID = oneObject.getInt("SourceId");

                    m.setMessageId(jsonMessageId);
                    m.setMessageText(jsonText);
                    m.setDate(jsonDate);
                    m.setAutor(jsonAutor);
                    m.setSource(jsonSource);
                    m.setThemeId(jsonThemeID);
                    m.setSourceId(jsonSourceID);

                    searchResult.add(m);

                } catch (JSONException e) {
                    Log.d("URL EXC", "Exception 2");
                }
            }
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
    return searchResult;
}

p.s. I use web-api as service and via android I take resources from service. Any idea where is my mistake here?

3
  • post your json string plz. Commented Jul 21, 2014 at 8:26
  • 1
    searchResult = new ArrayList<Message>(); should be outside the for loop. You are basically initializing the arraylist in looping . Hence only one result. Commented Jul 21, 2014 at 8:31
  • I edited but I still have the same problem... Commented Jul 21, 2014 at 8:39

1 Answer 1

3

You redefine your list every time in the loop.

Change your code from

 JSONArray jArray = new JSONArray(response);

        if (jArray != null) {

            for (int i = 0; i < jArray.length(); i++) {
                try {

                    searchResult = new ArrayList<Message>();

To

 JSONArray jArray = new JSONArray(response);

        if (jArray != null) {
            searchResult = new ArrayList<Message>();

            for (int i = 0; i < jArray.length(); i++) {
                try {
Sign up to request clarification or add additional context in comments.

2 Comments

I edited but still have the same problem... 1 message instead of 3 :(
Now I restarted eclipse and phone and it work :S Maybe it was problem that my app wasn't reinstalled properly but now everything is fine.

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.