本文介绍了安卓:JsonWriter不能写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用 JsonWriter 方法添加一些接收无线信息后test.json之后却成功写入文件,该文件仍然是空的。下面是codeS。

的onReceive

  @覆盖
        公共无效的onReceive(上下文C,意图我){
            如果(i.getAction()。等于(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
            {
                dbhelp =新DataBaseHelper(C);
                wifilist =新的ArrayList< ScanResult>();
                。wifilist =((WifiManager)c.getSystemService(Context.WIFI_SERVICE))getScanResults();
                dialog.dismiss();
                文件=新的文件(c.getExternalCacheDir(),testJson.json);
                尝试
                {
                    OUT =新的FileOutputStream(文件);
                    writeJson(出);
                }
                赶上(IOException异常E)
                {
                    e.printStackTrace();
                }
            }
        }

JsonWriter

 公共无效writeJson(OutputStream的了)抛出IOException
        作家=新JsonWriter(新OutputStreamWriter(满分,UTF-8));
        writer.setIndent();
        jsonFinal(作家);}公共无效jsonFinal(JsonWriter作家)抛出IOException
            writer.beginObject();
            writer.name(身份)值(OK)。
            writer.name(NUM_RESULTS)值()。
            writer.endObject();
         }

则预期结果应该像

  {
    状态:OK,
    NUM_RESULTS:,
}


解决方案

您忘记调用 JsonWriter.close(); 写在的JSONObject 做的:

  writer.name(状态)值(OK)。
        writer.name(NUM_RESULTS)值()。
        writer.endObject();
        writer.close(); //<<<关闭来电来访

i'm trying to call a JsonWriter method to add something to test.json after receiving wifi information, but after it successful write the file, the file still empty.Here are the codes.

OnReceive

 @Override
        public void onReceive(Context c, Intent i) {
            if (i.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
            {
                dbhelp = new DataBaseHelper(c);
                wifilist = new ArrayList<ScanResult>();
                wifilist = ((WifiManager) c.getSystemService(Context.WIFI_SERVICE)).getScanResults();
                dialog.dismiss();
                file = new File(c.getExternalCacheDir(),"testJson.json");
                try
                {
                    out = new FileOutputStream(file);
                    writeJson(out);
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }
            }
        }

JsonWriter

public void writeJson(OutputStream out) throws IOException {
        writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8"));
        writer.setIndent("    ");
        jsonFinal(writer);}

public void jsonFinal(JsonWriter writer) throws IOException{
            writer.beginObject();
            writer.name("status").value("OK");
            writer.name("num_results").value("");
            writer.endObject();
         }

the expected result should like

{
    "status": "OK",
    "num_results": "",
}
解决方案

you forget to call JsonWriter.close(); after writing JSONObject do it as:

        writer.name("status").value("OK");
        writer.name("num_results").value("");
        writer.endObject();
        writer.close();  //<<< call close here

这篇关于安卓:JsonWriter不能写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 22:07