xebia-functional / appsly-android-rest

An Android client library for RESTful based web services.
209 stars 51 forks source link

get chaced object #46

Open croccio opened 10 years ago

croccio commented 10 years ago

Hello! I'm using your android library to do rest request and to use cache. How can i get cached result? If i use annotation than when i call api.myrequest it give me null and not cached object. thank u.

package com.antonio.restrung;

import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button;

import org.apache.http.Header;

import java.io.IOException;

import ly.apps.android.rest.cache.CacheManager; import ly.apps.android.rest.cache.ContextPersistentCacheManager; import ly.apps.android.rest.client.Callback; import ly.apps.android.rest.client.Response; import ly.apps.android.rest.client.RestClient; import ly.apps.android.rest.client.RestClientFactory; import ly.apps.android.rest.client.RestServiceFactory;

public class MyActivity extends ActionBarActivity {

RestClient client = RestClientFactory.defaultClient(Application.getMyApplicationContext());
GetJson api = RestServiceFactory.getService("http://alessioarsuffipaybay.altervista.org", GetJson.class, client);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

int i = 0;
public void getJson(View v){

    api.getMioJson(new Callback<MioJson>(this) {

        @Override
        public void onResponse(Response<MioJson> mioJsonResponse) {
            //How get cached object
        }
    });
}

}

___-

package com.antonio.restrung;

import java.io.Serializable;

import ly.apps.android.rest.client.RestClient; import ly.apps.android.rest.client.RestClientFactory; import ly.apps.android.rest.client.RestServiceFactory;

/**

}


package com.antonio.restrung;

import ly.apps.android.rest.cache.CachePolicy; import ly.apps.android.rest.client.Callback; import ly.apps.android.rest.client.annotations.Cached; import ly.apps.android.rest.client.annotations.GET; import ly.apps.android.rest.client.annotations.QueryParam; import ly.apps.android.rest.client.annotations.RestService;

/**

}

raulraja commented 10 years ago

@croccio can you paste the code in the issue? thanks.

croccio commented 10 years ago

@raulraja done

raulraja commented 10 years ago

@croccio That type of cache policy only works if you have previously stored the response from a connected call otherwise it would return null. I recommend you use CachePolicy.ENABLED instead if you just want to load cached results at all times. The first time it will hit the network but subsequent calls would load from the cache. For more info on the cache policies take a look at https://github.com/47deg/appsly-android-rest#cache

croccio commented 10 years ago

@raulraja I've tried all policy and if 'm offline it return nuill. if i put this in on response method i get my cached object

try { Pair<Object, Boolean> cachedResult = new Pair<Object, Boolean>(getCacheManager().get(response.getCacheInfo().getKey(), response.getCacheInfo()), false); if (cachedResult.first != null) { Log.e("PAIR OBJECT", ((MioJson) cachedResult.first).getNome() + ""); }else{ Log.e("PAIR OBJECT", "Cache expired"); } } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }

raulraja commented 10 years ago

You are not supposed to use the CacheManager directly. Use response.getCacheInfo().isLoadedFromCache()

croccio commented 10 years ago

if i call that method it return true, but if i close app, turn off internet, open app, on response method return me null object. it return null object also if i'm offline without closing the app

alessioarsuffi commented 10 years ago

@croccio @raulraja i have the same problem, after first opening app and first call, i save a value in cache for a certain path, than if i remove internet connection onResume method gave me NULL, i'm pretty sure that cache shouldn't gave me null value... is there a problem of library ?

raulraja commented 10 years ago

Maybe some issue here https://github.com/47deg/appsly-android-rest/blob/master/android-rest/src/main/java/ly/apps/android/rest/cache/CacheAwareHttpClient.java#L123 but can't really tell if it's something specific to some devices or the Connectivity Manager. Turning off internet does not mean not being connected to a network. Do you guys see anything strange in the logcat?

croccio commented 10 years ago

here you are logcat. first while cache is valid, second after cache is INVALID

07-31 15:28:10.063 6948-6948/com.antonio.restrung D/android-rest﹕ main -> invoking: http://alessioarsuffipaybay.altervista.org/alessioarsuffipaybay_0001.json with body: null and temporary request content type: application/json 07-31 15:28:10.063 6948-6948/com.antonio.restrung D/android-rest﹕ main -> CacheAwareHttpClient.sendRequest 07-31 15:28:10.079 6948-6965/com.antonio.restrung D/android-rest﹕ AsyncTask #1 -> image read from disk 7a1b4733e23d18f6dfc39974cfb2971 07-31 15:28:10.079 6948-6965/com.antonio.restrung D/android-rest﹕ AsyncTask #1 -> Loading from cache because expiresOn : 1406813297361 > now : 1406813290088 07-31 15:28:10.094 6948-6948/com.antonio.restrung D/android-rest﹕ main -> CacheAwareHttpClient.sendRequest.onPostExecute proceeding with cache: android.util.Pair@4215eb7d 07-31 15:28:10.094 6948-6948/com.antonio.restrung D/android-rest﹕ main -> onSuccess: status200 rawResponse: null 07-31 15:28:10.094 6948-6948/com.antonio.restrung E/RESPONSE﹕ ly.apps.android.rest.client.Response@42160648 07-31 15:28:10.102 6948-6948/com.antonio.restrung D/android-rest﹕ main -> image read from disk 7a1b4733e23d18f6dfc39974cfb2971 07-31 15:28:10.102 6948-6948/com.antonio.restrung D/android-rest﹕ main -> Loading from cache because expiresOn : 1406813297361 > now : 1406813290111 07-31 15:28:10.102 6948-6948/com.antonio.restrung E/PAIR OBJECT﹕ ciao antonio 07-31 15:28:26.430 6948-6948/com.antonio.restrung D/android-rest﹕ main -> invoking: http://alessioarsuffipaybay.altervista.org/alessioarsuffipaybay_0001.json with body: null and temporary request content type: application/json 07-31 15:28:26.430 6948-6948/com.antonio.restrung D/android-rest﹕ main -> CacheAwareHttpClient.sendRequest 07-31 15:28:26.454 6948-6971/com.antonio.restrung D/android-rest﹕ AsyncTask #3 -> image read from disk 7a1b4733e23d18f6dfc39974cfb2971 07-31 15:28:26.454 6948-6971/com.antonio.restrung D/android-rest﹕ AsyncTask #3 -> Invalidated key : http://alessioarsuffipaybay.altervista.org/alessioarsuffipaybay_0001.json from cache because expiresOn : 1406813297361 < now : 1406813306463 07-31 15:28:26.454 6948-6948/com.antonio.restrung D/android-rest﹕ main -> CacheAwareHttpClient.sendRequest.onPostExecute proceeding uncached 07-31 15:28:26.485 6948-6948/com.antonio.restrung D/android-rest﹕ main -> CacheAwareCallback. Loading from cache after response error: UnknownHostException exception: Unable to resolve host "alessioarsuffipaybay.altervista.org": No address associated with hostname result with cached result: null 07-31 15:28:26.485 6948-6948/com.antonio.restrung D/android-rest﹕ main -> onFailure: status0 rawResponse: null 07-31 15:28:26.485 6948-6948/com.antonio.restrung E/RESPONSE﹕ ly.apps.android.rest.client.Response@420fd0a8 07-31 15:28:26.485 6948-6948/com.antonio.restrung D/android-rest﹕ main -> image read from disk 7a1b4733e23d18f6dfc39974cfb2971 07-31 15:28:26.485 6948-6948/com.antonio.restrung D/android-rest﹕ main -> Invalidated key : http://alessioarsuffipaybay.altervista.org/alessioarsuffipaybay_0001.json from cache because expiresOn : 1406813297361 < now : 1406813306495 07-31 15:28:26.485 6948-6948/com.antonio.restrung E/PAIR OBJECT﹕ Cache expired

croccio commented 10 years ago

it cannot connect to url to get json and so it doesn't return cached object.

raulraja commented 10 years ago

I don't think that is the reason. It invalidated the cache previous to the returned value becase your second request was 9 seconds after your timetolive.

Invalidated key : http://alessioarsuffipaybay.altervista.org/alessioarsuffipaybay_0001.json 
from cache because expiresOn : 1406813297361 < now : 1406813306463
croccio commented 10 years ago

ok, but if the cache is valid it return me null

croccio commented 10 years ago

here it return me null

07-31 15:28:10.063 6948-6948/com.antonio.restrung D/android-rest﹕ main -> invoking: http://alessioarsuffipaybay.altervista.org/alessioarsuffipaybay_0001.json with body: null and temporary request content type: application/json 07-31 15:28:10.063 6948-6948/com.antonio.restrung D/android-rest﹕ main -> CacheAwareHttpClient.sendRequest 07-31 15:28:10.079 6948-6965/com.antonio.restrung D/android-rest﹕ AsyncTask #1 -> image read from disk 7a1b4733e23d18f6dfc39974cfb2971 07-31 15:28:10.079 6948-6965/com.antonio.restrung D/android-rest﹕ AsyncTask #1 -> Loading from cache because expiresOn : 1406813297361 > now : 1406813290088 07-31 15:28:10.094 6948-6948/com.antonio.restrung D/android-rest﹕ main -> CacheAwareHttpClient.sendRequest.onPostExecute proceeding with cache: android.util.Pair@4215eb7 07-31 15:28:10.094 6948-6948/com.antonio.restrung D/android-rest﹕ main -> onSuccess: status200 rawResponse: null 07-31 15:28:10.094 6948-6948/com.antonio.restrung E/RESPONSE﹕ ly.apps.android.rest.client.Response@4216064

croccio commented 10 years ago

and now cache isn't invalidate because:

Loading from cache because expiresOn : 1406813297361 > now : 1406813290088

raulraja commented 10 years ago

Do you see the same behavior with CachePolicy.ENABLED ?

croccio commented 10 years ago

yer. i've same result with all policy

raulraja commented 10 years ago

I'm not sure what the issue is. It appears as if the cache had no effect in your terminal. Can you try with a different device?

croccio commented 10 years ago

i've the problem with galaxy nexus(pa android 4.4), nexus 4(stock android 4.4) and lg optimus one (p500with android 2.3.7 cm)

croccio commented 10 years ago

if you would i can try on s3 and s2 with stock firmaware

croccio commented 10 years ago

can you tell me how can i get cached object here? maybe i'm doing something wrong

api.getMyJson(new Callback(this) {

    @Override
    public void onResponse(Response<MyJson> myJsonResponse) {
        //How get cached object
    }
});
raulraja commented 10 years ago

You don't need to do anything there myJsonResponse.result is the cached object. If you want to know if it was loaded from the cache or not use myJsonResponse.getCacheInfo().isLoadedFromCache()

croccio commented 10 years ago

Ok. If i do so, it give me null on 5 device. Maybe there is a problem in library. I'll try to see it.