当我尝试保存有关json arrayList的信息时出现问题

时间:2018-11-05 19:23:48

标签: android

所以我有这个应用程序,它从我的主要活动中获取2编辑文本中的信息到我的第二个活动中,在其中我将其保存在json上,使用该信息创建一个arraylist并将其显示在ListView上。但是,当我尝试在应用程序上保存任何信息时。我得到了这个日志猫:

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)

我遇到麻烦的代码是这个。

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();
}
}

这是我的班级日记,我在其中做json和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;
}

}

谢谢。

0 个答案:

没有答案