2 回答

TA貢獻1818條經驗 獲得超11個贊
這個問題有很多可能的答案。您可以將數據本地存儲在 SQLite 數據庫中,然后可以在需要時從那里讀取數據。如果您的數據可能經常更改,那么這不是一個好主意。
另一種方法是將數據作為對象在活動之間傳遞。如果數據在活動轉換期間不太可能發生更改,我認為這是一個好主意。這樣,就不用每次都從網絡加載數據了。因此,一旦在 中讀取數據,就可以將其作為對象或意圖MainActivity
傳遞給。CardActivity

TA貢獻2011條經驗 獲得超2個贊
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
? ? ? ? ? ? ? ? (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onResponse(JSONObject response) {
? ? ? ? ? ? ? ? ? ? ? ? ...
? ? ? ? ? ? ? ? }, new Response.ErrorListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onErrorResponse(VolleyError error) {
? ? ? ? ? ? ? ? ? ? ? ? ...
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }) {
? ? ? ? ? ? @Override
? ? ? ? ? ? protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);
? ? ? ? ? ? ? ? ? ? if (cacheEntry == null) {
? ? ? ? ? ? ? ? ? ? ? ? cacheEntry = new Cache.Entry();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? final long cacheHitButRefreshed = 3 * 60 * 1000; // in 3 minutes cache will be hit, but also refreshed on background
? ? ? ? ? ? ? ? ? ? final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
? ? ? ? ? ? ? ? ? ? long now = System.currentTimeMillis();
? ? ? ? ? ? ? ? ? ? final long softExpire = now + cacheHitButRefreshed;
? ? ? ? ? ? ? ? ? ? final long ttl = now + cacheExpired;
? ? ? ? ? ? ? ? ? ? cacheEntry.data = response.data;
? ? ? ? ? ? ? ? ? ? cacheEntry.softTtl = softExpire;
? ? ? ? ? ? ? ? ? ? cacheEntry.ttl = ttl;
? ? ? ? ? ? ? ? ? ? String headerValue;
? ? ? ? ? ? ? ? ? ? headerValue = response.headers.get("Date");
? ? ? ? ? ? ? ? ? ? if (headerValue != null) {
? ? ? ? ? ? ? ? ? ? ? ? cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? headerValue = response.headers.get("Last-Modified");
? ? ? ? ? ? ? ? ? ? if (headerValue != null) {
? ? ? ? ? ? ? ? ? ? ? ? cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? cacheEntry.responseHeaders = response.headers;
? ? ? ? ? ? ? ? ? ? final String jsonString = new String(response.data,
? ? ? ? ? ? ? ? ? ? ? ? ? ? HttpHeaderParser.parseCharset(response.headers));
? ? ? ? ? ? ? ? ? ? return Response.success(new JSONObject(jsonString), cacheEntry);
? ? ? ? ? ? ? ? } catch (UnsupportedEncodingException | JSONException e) {
? ? ? ? ? ? ? ? ? ? return Response.error(new ParseError(e));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? protected void deliverResponse(JSONObject response) {
? ? ? ? ? ? ? ? super.deliverResponse(response);
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void deliverError(VolleyError error) {
? ? ? ? ? ? ? ? super.deliverError(error);
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? protected VolleyError parseNetworkError(VolleyError volleyError) {
? ? ? ? ? ? ? ? return super.parseNetworkError(volleyError);
? ? ? ? ? ? }
? ? ? ? };
希望這會對某人有所幫助
添加回答
舉報