簡體   English   中英

沒有網絡時寫入文件和從文件讀取

[英]Writing to file and reading from file when no internet

如果沒有互聯網,如何將數據從Web API寫入文件,以及如何從該文件讀取數據?

public static boolean isConnectingToInternet(Context _context) {
        ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }

        }
        return false;
    }

您不能在離線模式下請求Web服務。 最好的辦法是,您可以應用客戶現金(在請求標頭中設置max-age,s-maxage,max-stale),或僅將來自Web api的數據保存在本地數據庫(如sqlite或Realm)中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM