2

I'm using the Gson library to save and retrieve an ArrayList of Players Objects.


My onStop()

@Override
protected void onStop() {
    super.onStop();
    SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    Gson gson = new Gson();

    String guardJSON = gson.toJson(playersNoGuard);
    editor.putString(GUARD, guardJSON);

    editor.putString("lastActivity", getClass().getName());
    editor.apply();
}

My onCreate important part

ArrayList<Player> playersNoGuard;
RecyclerView myList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_players_guard);

    SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
    String guardJSON = prefs.getString(GUARD, null);
    Type arrayListPlayers = new TypeToken<ArrayList<Player>>(){}.getType();
    Gson gson = new Gson();

    if(guardJSON != null) {
        playersNoGuard = gson.fromJson(guardJSON, arrayListPlayers);
    }

    // Get the players and remove the Clairvoyant
    Intent intent = this.getIntent();
    playersNoGuard = intent.getParcelableArrayListExtra("PLAYERS");

    [...] // Code skipped

    }

But when this Activity is run, I get the following error log:

Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

What's wrong in this code?

10
  • your json is an object not an array Commented Jul 22, 2016 at 11:24
  • Possible duplicate of GSON Expected BEGIN_ARRAY but was BEGIN_OBJECT Commented Jul 22, 2016 at 11:27
  • I' not sure that's my specific case Commented Jul 22, 2016 at 13:45
  • Why not?! @TimCastelijns Commented Jul 22, 2016 at 14:59
  • How am I supposed to know that.. it's your data not mine Commented Jul 22, 2016 at 15:01

2 Answers 2

1

You probably write to the same preferences entry somewhere else. Ensure that your key (GUARD) is unique throughout the application.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is with your Response while Gson converting a response it expected Array in response but in your response you have made you are passing Object.

This is already answered here and here

here is reverse situation described.

3 Comments

Why am I passing an Object? I saved it as an ArrayList, I've used this same code for other ArrayLists and I get no errors
This is already answered here and here why dont you flag for duplicate instead of posting an answer then
Guys, all these examples are not helping in my case, or at least I don't see how. I've used this code in other Activities and it's working fine. what did I do wrong?

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.