gpt4 book ai didi

java - Android:JsonWriter 无法写入文件

转载 作者:行者123 更新时间:2023-11-29 05:36:45 25 4
gpt4 key购买 nike

我试图在收到 wifi 信息后调用 JsonWriter 方法向 test.json 添加一些内容,但是在成功写入文件后,文件仍然是空的。这是代码。

接收时

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

预期的结果应该是这样的

{
"status": "OK",
"num_results": "",
}

最佳答案

你忘记调用 JsonWriter.close(); 在写完 JSONObject 之后做它:

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

关于java - Android:JsonWriter 无法写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19277529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com