简体   繁体   English

从json文件BufferedReader读取数据仅返回null

[英]Reading data from a json file BufferedReader returns only null

I'm currently learning android with a books called "Android Programming - The Big Nerd Ranch Guide". 我目前正在学习一本名为“ Android编程-The Big Nerd Ranch Guide”的书。 As a part of a learning project we create Json serializer for saving and loading data. 作为学习项目的一部分,我们创建了Json序列化器来保存和加载数据。 Writing the file appearently works fine, and I get no error messages on the Logcat. 写入文件似乎正常,并且Logcat上没有错误消息。 After I terminate the app and recreate it, the data loader is called and raises the following exception: 在终止应用程序并重新创建应用程序之后,将调用数据加载器并引发以下异常:

org.json.JSONException: End of input at character 0 org.json.JSONException:字符0的输入结束

I've looked for this issue online and figured it's probably because the BufferedReader returns an empty response. 我已经在网上查找此问题,并且发现这可能是因为BufferedReader返回空响应。 I've checked and indeed it is the case. 我已经检查过了,的确如此。 For simplicity sake, I've temporarily put a BufferedReader into the saving function and tried reading the info I've just saved into the file, and still the BufferedReader returns only null. 为了简单起见,我暂时将BufferedReader放入保存功能,并尝试读取刚刚保存到文件中的信息,但BufferedReader仍然仅返回null。

public void saveCrimes(ArrayList<Crime>crimes)
    throws JSONException, IOException {
    JSONArray array = new JSONArray();
    for(Crime c: crimes)
        array.put(c.toJSON());
    Writer writer = null;
    try {
        OutputStream out = mContext.openFileOutput(mFileName, Context.MODE_PRIVATE);
        writer = new OutputStreamWriter(out);
        writer.write(array.toString());
        Log.d(TAG, array.toString());
    } finally {
        if(writer == null)
            writer.close();
    }
    // Extracting the data
    BufferedReader bufferedReader = null;
    try {
        InputStream inputStream = mContext.openFileInput(mFileName);
        bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        if (bufferedReader.readLine() == null)
            Log.d(TAG, "WHY GOD WHYYYYYYY");
    }catch (IOException e){

    }
}

(The two log messages from the code, the first one displays the data that is in the JsonArray I'm using) (代码中的两个日志消息,第一个显示我正在使用的JsonArray中的数据)

D/CriminalIntentJSONSerializer: [{"date":"Mon May 14 17:33:08 GMT+00:00 2018","id":"97fe9532-991f-4352-9de1-602fa8dfa93e","isSolved":true,"title":""}] D/CriminalIntentJSONSerializer: WHY GOD WHYYYYYYY D / CriminalIntentJSONSerializer:[{“ date”:“ Mon May 14 17:33:08 GMT + 00:00 2018”,“ id”:“ 97fe9532-991f-4352-9de1-602fa8dfa93e”,“ isSolved”:true,“ title“:”“}] D / CriminalIntentJSONSerializer:WHY GOD WHYYYYYYY

Would love to hear your insight. 很想听听您的见解。

BufferedReader bufferedReader = null;
try {
    InputStream inputStream = mContext.openFileInput(mFileName);
    bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    if (bufferedReader.readLine() == null)
        Log.d(TAG, "WHY GOD WHYYYYYYY");
}catch (IOException e){

}

Ok. 好。 you've created your BufferedReader bufferedReader = null; 您已经创建了BufferedReader bufferedReader = null; What happens when yuou say bufferedReader = new BufferedReader(anything) ...Well...it can't call a new instance the same thing that's already been declared...in fact, it's already been instantiated as null. 当您说bufferedReader = new BufferedReader(anything) ...怎么办...它不能调用新实例的方式与已经声明的内容相同...实际上,它已被实例化为null。 So you can't create a new instance of the same name. 因此,您无法创建具有相同名称的新实例。

Try deleting the line where you point it at null. 尝试删除指向空的行。 Then, substitute BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 然后,替换BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); for the original declaration in your try block 用于try块中的原始声明

Try to check if mFilename is empty on second try-catch , usually in Android instance disappear easily. 尝试在第二次try-catch上检查mFilename是否为空,通常在Android实例中很容易消失。

PD: I advise you choice another JSON library to manipulte JSON files, they are lightweight and easy-to-use . PD:我建议您选择另一个JSON库来操作JSON文件,这些文件轻巧且easy-to-use

== Edit == ==编辑==

Have you added writing and reading permissions on AndroidManifest? 您是否已在AndroidManifest中添加了读写权限? If answer is "there it is" try to debug app step-by-step looking for variables and in-variables for checking existence of values. 如果答案是“有”,请尝试逐步调试应用程序,以查找变量和不变量以检查值的存在。

Could be file isn't writing itself or it's writing empty. 可能是文件未写自身或写为空。

Error basically is empty string or non-format JSON-like: 错误基本上是空字符串或非格式的类似于JSON的错误:

""
"[{"a": "abdc", "b": "jef2","

Paying attention to BufferedReader because it read lines each and you need all file and then join into string variable. 注意BufferedReader,因为它读取每一行并且您需要所有文件,然后加入字符串变量。

Also, try to use android file explorer that come in AndroidStudio. 另外,尝试使用AndroidStudio中随附的android文件资源管理器。 There you can explore files, logs and database files and export them to your specific folders (Documents, Downloads, etc). 在这里,您可以浏览文件,日志和数据库文件,并将它们导出到您的特定文件夹(文档,下载等)。 Generally files written by app are stored in data -> <com.your.app.package> . 通常,由app编写的文件存储在data -> <com.your.app.package>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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