首页 > 解决方案 > 从内部存储器存储和检索对象

问题描述

我正在尝试使用以下代码将对象存储在内部存储器中。我能够将对象保存在内部存储器中

  private void writeAppSpecificExternalFile(Context context, boolean isPersistent, Object obj) {
    String fileName = "credential";
    File file;
    if (isPersistent) {
        file = new File(Environment.getExternalStorageDirectory(), fileName);
    } else {
        file = new File(Environment.getExternalStorageDirectory(), fileName);
    }
    try {
        Gson gson = new Gson();
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(gson.toJson(obj).getBytes());
        Toast.makeText(context, String.format("Write to %s successful", obj.toString()), Toast.LENGTH_SHORT).show();
        Log.d("CREDD", obj.toString());
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(context, String.format("Write to file %s failed", fileName), Toast.LENGTH_SHORT).show();
    }
}

我正在尝试使用以下代码将其取回

 private void readAppSpecificExternalFile(Context context, boolean isPersistent) {
    File file;
    String fileName = "credential";
    if (isPersistent) {
        file = new File(Environment.getExternalStorageDirectory(), fileName);
    } else {
        file = new File(Environment.getExternalStorageDirectory(), fileName);
    }
    try {
        FileInputStream fileInputStream = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, Charset.forName("UTF-8"));
        List<String> lines = new ArrayList<String>();
        BufferedReader reader = new BufferedReader(inputStreamReader);
        String line = reader.readLine();
        while (line != null) {
            lines.add(line);
            line = reader.readLine();
        }
        String fullCred = TextUtils.join("\n", lines);
        Gson gson = new Gson();
        JsonParser parser=new JsonParser();
        Object obj = gson.fromJson(parser.parse(fullCred), Object.class);
        Toast.makeText(context, String.format("Read from file %s successful", obj.toString()), Toast.LENGTH_SHORT).show();
        Log.d("CREDD", obj.toString());

    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(context, String.format("Read from file %s failed", "tets"), Toast.LENGTH_SHORT).show();
    }
}

当我尝试解析它并将其转换为对象时,它会引发以下错误

    /corporate.toke: Accessing hidden field Lsun/misc/Unsafe;->theUnsafe:Lsun/misc/Unsafe; (light greylist, reflection)
W/System.err: java.lang.RuntimeException: Unable to invoke no-args constructor for interface com.x.x.x Registering an InstanceCreator with Gson for this type may fix this problem.
W/System.err:     at com.google.gson.internal.ConstructorConstructor$14.construct(ConstructorConstructor.java:226)
W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:210)
W/System.err:     at com.google.gson.Gson.fromJson(Gson.java:888)
W/System.err:     at com.newrelic.agent.android.instrumentation.GsonInstrumentation.fromJson(GsonInstrumentation.java:126)
W/System.err:     at com.google.gson.Gson.fromJson(Gson.java:953)
W/System.err:     at com.newrelic.agent.android.instrumentation.GsonInstrumentation.fromJson(GsonInstrumentation.java:144)
W/System.err:     at com.google.gson.Gson.fromJson(Gson.java:926)
W/System.err:     at com.newrelic.agent.android.instrumentation.GsonInstrumentation.fromJson(GsonInstrumentation.java:135)
W/System.err:     at com.frbcorporate.token.ui.splash.SplashActivity.readAppSpecificExternalFile(SplashActivity.java:132)
W/System.err:     at com.frbcorporate.token.ui.splash.SplashActivity.onCreate(SplashActivity.java:108)
W/System.err:     at android.app.Activity.performCreate(Activity.java:7136)
W/System.err:     at android.app.Activity.performCreate(Activity.java:7127)
W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
W/System.err:     at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
W/System.err:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
W/System.err:     at android.os.Looper.loop(Looper.java:193)

我是否需要使用检索到的字符串值创建一个新实例,而不是使用 gson 进行解析?

标签: androidserializationgsonparcelable

解决方案


推荐阅读