简体   繁体   中英

Different results parsing the same JSON

I'm having a hard time figuring out what's causing my problem.

So, I'm trying to get two groups of strings from a JSON and show them on a listview. When I test the app for the first time, it's great. Everything works as expected.

But when I close, and reopen it, my listview shows only one group of strings (only the second one, which is repeated two times).

This is the class that handles parsing the JSON: (It's executed inside an AsyncTask)

    public class ListarPosts extends SherlockActivity{

    String URL = null, Titulo = null;

    ArrayList<Post> posts = new ArrayList<Post>();

    public ArrayList<Post> getPosts(String Json){
        try {
            JSONObject jObject = new JSONObject(Json);

            JSONArray jArray = jObject.getJSONArray("Posts");

            for (int i = 0; i < jArray.length(); i++) {
                final int id = jArray.getJSONObject(i).getInt("ID");
                final int tipo = verificaTipo(jArray.getJSONObject(i).getString("Tipo"));
                final JSONArray jValues = jArray.getJSONObject(i).getJSONArray("Valores");

                for (int a = 0; a < jValues.length(); a++) {
                    final String KEY = jValues.getJSONObject(a).getString("Key");
                    System.out.println(KEY);
                    final String Value = jValues.getJSONObject(a).getString("Value");
                    System.out.println(Value);

                    if (KEY.equals("URL"))
                        URL = Value;
                    else if (KEY.equals("TITULO"))
                        Titulo = Value;
                }

                ListarPosts.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        posts.add(new Post(id, Titulo, tipo));
                    }

                });
            }
            return posts;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

When I print the key and the value, it shows the correct strings, even if the listview is wrong.

My guess is that something is wrong when I create the new "Post" and add it to the ArrayList. That would explain why it prints the right one, but doesn't show it after at the ListView. Maybe some conflict because I'm creating a new instance inside an AsyncTask?

Anyway, I'm a bit lost. If you have any idea of what I should try or what might be wrong, please let me know.

Thanks for any help at all!

just clear every time Arraylist before Add to new Data like this.... and also remove runonuithread in Asynctask.thats no need here..

public ArrayList<Post> getPosts(String Json){
        try {
            JSONObject jObject = new JSONObject(Json);

            JSONArray jArray = jObject.getJSONArray("Posts");


                  if(posts.size()>0){
                  posts.clear();
             }

            for (int i = 0; i < jArray.length(); i++) {
                final int id = jArray.getJSONObject(i).getInt("ID");
                final int tipo = verificaTipo(jArray.getJSONObject(i).getString("Tipo"));
                final JSONArray jValues = jArray.getJSONObject(i).getJSONArray("Valores");


                for (int a = 0; a < jValues.length(); a++) {
                    final String KEY = jValues.getJSONObject(a).getString("Key");
                    System.out.println(KEY);
                    final String Value = jValues.getJSONObject(a).getString("Value");
                    System.out.println(Value);

                    if (KEY.equals("URL"))
                        URL = Value;
                    else if (KEY.equals("TITULO"))
                        Titulo = Value;
                }


                 posts.add(new Post(id, Titulo, tipo));
             }
            return posts;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

thats it....

Problem is in runOnUiThread(). The value of the Tipo is updated in the thread I think.

  public class ListarPosts extends Activity {

        ArrayList<String> URL = null, Titulo = null;

        ArrayList<Post> posts = new ArrayList<Post>();
        int id;
        int tipo;

        public ArrayList<Post> getPosts(String Json) {

            try {
                posts = new ArrayList<Post>();
                URL = new ArrayList<String>();
                Titulo = new ArrayList<String>();
                JSONObject jObject = new JSONObject(Json);

                JSONArray jArray = jObject.getJSONArray("Posts");

                for (int i = 0; i < jArray.length(); i++) {
                    id = jArray.getJSONObject(i).getInt("ID");
                    tipo = 1;
                    final JSONArray jValues = jArray.getJSONObject(i).getJSONArray(
                            "Valores");

                    for (int a = 0; a < jValues.length(); a++) {
                        final String KEY = jValues.getJSONObject(a)
                                .getString("Key");
                        System.out.println(KEY);
                        final String Value = jValues.getJSONObject(a).getString(
                                "Value");
                        System.out.println(Value);

                        if (KEY.equals("URL"))
                            URL.add(Value);
                        else if (KEY.equals("TITULO"))
                            Titulo.add(Value);
                    }

                }
                for (String value : Titulo) {
                        // If you need to use runOnUiThread, you can implement here
                    posts.add(new Post(id, value, tipo));
                }
                return posts;
            } catch (ParseException e) {
                e.printStackTrace();
                return null;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
          }
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM