第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何緩存/讀取 json 文件?

如何緩存/讀取 json 文件?

慕無忌1623718 2022-11-02 15:14:32
我在嘗試緩存和讀取 json 文件時(shí)遇到問題,我使用 google dev 的示例緩存文件,然后嘗試使用不同的函數(shù)讀取文件readCache(),但出現(xiàn)錯(cuò)誤outputStream.read() -> 無法解析方法 read().....有人可以解釋一下我在做什么錯(cuò)嗎?private void cacheFile(JSONObject response) {     JSONObject res  = response;     String filename = "jsonfile";     FileOutputStream outputStream;     try {         outputStream = openFileOutput(filename, Context.MODE_PRIVATE);         outputStream.write(res.toString().getBytes("utf-8"));         outputStream.close();         Log.e(TAG, "Success");     } catch (Exception e) {         e.printStackTrace();     }}private void readCache(String filename) {    FileOutputStream outputStream;    try {        outputStream = openFileOutput(filename, Context.MODE_PRIVATE);        outputStream.read();        outputStream.close();    } catch (Exception e) {        e.printStackTrace();    }}
查看完整描述

1 回答

?
森欄

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊

嘗試將您的readCache()方法更改為


private void readCache(String filename) {

    FileInputStream inputStream;

    try {

        inputStream = openFileInput(filename);

        String body = inputStreamToString(inputStream);

        inputStream.close();

    } catch (Exception e) {

        e.printStackTrace();

    }

}


private String inputStreamToString(InputStream inputStream) throws Exception {

    ByteArrayOutputStream result = new ByteArrayOutputStream();

    byte[] buffer = new byte[1024];

    int length;

    while ((length = inputStream.read(buffer)) != -1) {

        result.write(buffer, 0, length);

    }

    return result.toString("UTF-8");

}


查看完整回答
反對(duì) 回復(fù) 2022-11-02
  • 1 回答
  • 0 關(guān)注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)