1

So i have this app that takes an info from 2 edittext from my main activity to my second Activity where i save it on json, create an arraylist with that info and show it on a ListView. But when i try to save any info on the app. i got this logcat:

Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
    at com.google.gson.Gson.fromJson(Gson.java:939)
    at com.google.gson.Gson.fromJson(Gson.java:892)
    at com.google.gson.Gson.fromJson(Gson.java:841)
    at com.example.alexj.ejercicio6.Journal.fromJson(Journal.java:43)
    at com.example.alexj.ejercicio6.ListViewActivity.onCreate(ListViewActivity.java:34)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)

The code what im getting troubles with is this one.

public class ListViewActivity extends AppCompatActivity {

SharedPreferences sharedPreferences;
List<Journal> journalList;


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

    sharedPreferences = getSharedPreferences("Journal", MODE_PRIVATE);
    String json = sharedPreferences.getString("journal", "");
    Journal journals = new Journal();

    if (json !=null && !TextUtils.isEmpty(json)){
        Type type = new TypeToken<List<Journal>>(){}.getType();
        journalList = journals.fromJson(json, type);
    }

    Intent intent = getIntent();
    String newPlace= intent.getStringExtra("newPlace");
    String newDescription = intent.getStringExtra("newDescription");

    if(journalList == null){
        journalList = new ArrayList<>();
    }
    if(newPlace != null && !TextUtils.isEmpty(newPlace) && newDescription !=null &&
            !TextUtils.isEmpty(newDescription)){
        boolean listFound = false;
        for (Journal journal1 : journalList){
            if (journal1.getPlace().equalsIgnoreCase(newPlace)){
                listFound = true;
            }
        }
        if (listFound){
            Toast.makeText(this, "You already wrote about that places", Toast.LENGTH_SHORT).show();
        } else {
            journalList.add(new Journal(newPlace, newDescription));
            json = journals.toJson(journalList);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("journal", json);
            editor.apply();
        }
    }

    ListView list = findViewById(R.id.list);
    MyAdapter myAdapter = new MyAdapter(journalList, this);
    list.setAdapter(myAdapter);


}
public void back (View view) {
    finish();
}
}

And this is my class journal where i do my json and gson.

public class Journal {

private String place;
private String description;

public Journal(String place, String description){
    this.place = place;
    this.description = description;
}

public Journal() {
}

public String getPlace(){
    return place;
}
public String getDescription(){
    return description;
}

public String toJson(List<Journal> journalList){
    Gson gson = new Gson();
    String json = gson.toJson(journalList);
    return json;
}
public List<Journal> fromJson(String json, Type type){
    Gson gson = new Gson();
    List<Journal> journals = gson.fromJson(json, type);
    return journals;
}

}

Thank you.

  • check this answer https://stackoverflow.com/questions/9598707/gson-throwing-expected-begin-object-but-was-begin-array – MB_Coder Nov 05 '18 at 19:54

0 Answers0