Retrofit response : Cache Control. What does it mean? - android

I use retrofit and picasso library. Picasso manages caching itself. But when I view logcat, I see log below. What does it mean? Doesn't the webservice and backend send cache info correctly? How can I solve this to make Retrofit cache correctly. So, Does this data make sense?
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Content-Type: application/json
Date: Wed, 14 Dec 2016 07:15:48 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
OkHttp-Received-Millis: 1481597410805
OkHttp-Response-Source: NETWORK 200
OkHttp-Selected-Protocol: http/1.1
OkHttp-Sent-Millis: 1481597409021
Pragma: no-cache
Server: Apache
Transfer-Encoding: chunked

You can set cache option to OkHttp with using Interceptor.
OkHttpClient client = new OkHttpClient();
client.networkInterceptors().add(new CachingControlInterceptor());
Retrofit restAdapter = new Retrofit.Builder()
.client(client)
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
and CachingControlInterceptor is:
public class CachingControlInterceptor implements Interceptor {
#Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
// Add Cache Control only for GET methods
if (request.method().equals("GET")) {
if (ConnectivityUtil.checkConnectivity(YaootaApplication.getContext())) {
// 1 day
request = request.newBuilder()
.header("Cache-Control", "only-if-cached")
.build();
} else {
// 4 weeks stale
request = request.newBuilder()
.header("Cache-Control", "public, max-stale=2419200")
.build();
}
}
Response originalResponse = chain.proceed(request);
return originalResponse.newBuilder()
.header("Cache-Control", "max-age=600")
.build();
}
}
See this:
https://stackoverflow.com/a/34401686/850347

Related

OkHttp3 HttpLoggingInterceptor log body without logging header

I'm building an Android app, which uses Retrofit for the networking.
Here's the code:
private val loggingInterceptor by lazy {
HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { message -> Log.d(TAG, message) }).apply {
level = if(BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE
}
}
val client = OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build()
Retrofit.Builder()
.baseUrl(" https://mybaseurl.com/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
I use an HttpLoggingInterceptor, however logs are too verbose because it logs all the headers..
content-language: en-GB
content-type: application/json
date: Sun, 07 Jul 2019 12:52:36 GMT
p3p: CP="NON CUR OTPi OUR NOR UNI"
transfer-encoding: chunked
cache-control: no-cache,no-store,must-reva
x-powered-by: Servlet/3.0
expires: Thu, 01 Dec 1994 16:00:00 GMT
pragma: no-cache
and more....
If I switch to LogLevel Basic though, I don't get the request and response, but just the URLs.
Btw, looking inside HttpLoggingInterceptor code:
boolean logHeaders = logBody || level == Level.HEADERS;
So it seems that there is no actual way to log body without the headers.
Is there any hacky way or workaround?

Retrofit 504 Unsatisfiable Request (only-if-cached)

I'm trying to use Caching using Retrofit and OkHttp.
I'm adding this interceptor to OkHttp builder like.
#Provides
#Singleton
#TokenHttpClient
internal fun provideTokenOkHttpClient(
#BaseHttpClient okHttpClient: OkHttpClient,
cacheInterceptor: NetworkCacheInterceptor): OkHttpClient {
return okHttpClient.newBuilder()
.addInterceptor(cacheInterceptor)
.build()
And the interceptor looks like this:
class NetworkCacheInterceptor(val context: Context): Interceptor {
#Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
var response = chain.request()
response = if (hasNetwork(context))
response.newBuilder()
.removeHeader("Pragma")
.removeHeader("Cache-Control")
.header("Cache-Control",
"public, max-age=${CacheControl.Builder().maxAge(30, TimeUnit.DAYS).build()}")
.build()
else {
response.newBuilder()
.removeHeader("Pragma")
.removeHeader("Cache-Control")
.header("Cache-Control",
"public, only-if-cached, max-stale=${CacheControl.Builder().maxAge(30, TimeUnit.DAYS).build()}")
.cacheControl(CacheControl.FORCE_CACHE)
.build()
response
}
return chain.proceed(response)
}
}
This way of implementation it's saving responses into cache folder. But it's not working when offline. I've tired to separate interceptors and add the caching as networkInterceptor and the offline interceptor as normal interceptor but it didn't work. On this case no cache was saved.
When I check cached files I see these headers:
GET
0
HTTP/1.1 200
15
Access-Control-Expose-Headers: value
Cache-Control: no-cache
ETag: "01785cb0981d28aa96d58e491dbd6873e"
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
X-Frame-Options: SAMEORIGIN
Referrer-Policy: no-referrer
Content-Type: application/json;charset=UTF-8
Date: Tue, 21 May 2019 07:50:51 GMT
Transfer-Encoding: chunked
Content-Encoding: gzip
OkHttp-Sent-Millis: 1558425050896
OkHttp-Received-Millis: 1558425050928
Any idea? What I'm missing?

Webservice is giving error 401 in Android Retrofit 2, but works in Volley and Postman

I have been using retrofit and auth for a while with no problem.
But from this API call while everything is exactly the same as my other projects which works, I'm constantly getting error code 401
here is my API calling code:
#GET("workers")
fun getWorkersList(): Observable<BaseResponseModel<WorkerAttributes>>
companion object Factory {
fun create(): WebServices {
val okHttpClientBuilder = OkHttpClient.Builder()
val httpLoggingInterceptor = HttpLoggingInterceptor()
httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
okHttpClientBuilder.addInterceptor(httpLoggingInterceptor)
okHttpClientBuilder.addInterceptor { chain ->
val request = chain.request().newBuilder()
request.addHeader("Authorization", "Bearer eyJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiYWxnIjoiUlNBLU9BRVAiLCJraWQiOiIzaGltWWEyYmJHS18zSVdZMUxUUHdyTUl2ZkF3ZVhZQmRNLTU5XzN6c0MwIn0.DIwk0Q215_M7rt3iGfyRVrImhKCXvEB5NBoLQj6iqBnN9yCWWBhGW9fmX6beOajtWgC8gpupe1hJ-iurFRbo2Eg52Nh9ljkkO5Mz8ulSpH7YlfP8Yi-TBrNoPIf_IvHrr1Rt8bG0w2Ie1Jo3BBOguMQ6Q2WI4fQDY-oFIluxOQ5t9E0_XvDhna7maiQnWevLbu80Mj7g643v5nvNWOdpSAXW91rZXIXdal_A_ffY-IUyvYhEJuOkyHWwNonuGjiGHWKPTjTR16w3seoSs4srkacOIN1cmMdXKlkvlWSYCv5Nd8xJ_rTqA4GIyKL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XzEw0gT4HbLqcFfdWoFQ")
request.addHeader("Content-Type", "application/json")
request.addHeader("X-Requested-With", "XMLHttpRequest")
request.addHeader("Cookie", "XMLHttpRequest")
chain.proceed(request.build())
}
val retrofit = Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClientBuilder.build())
.baseUrl(Constants.BASE_URL)
.build()
return retrofit.create(
WebServices::
class.java
)
}
}
and here is my error:
D/OkHttp: --> GET http://pilot.wakecap.com/api/sites/10010001/workers
D/OkHttp: --> END GET
I/art: Do partial code cache collection, code=60KB, data=57KB
I/art: After code cache collection, code=58KB, data=56KB
I/art: Increasing code cache capacity to 256KB
D/OkHttp: <-- 401 Unauthorized https://pilot.wakecap.com/api/sites/10010001/workers (2645ms)
D/OkHttp: Server: nginx/1.14.0 (Ubuntu)
D/OkHttp: Date: Wed, 16 Jan 2019 20:16:44 GMT
D/OkHttp: Content-Type: application/json; charset=utf-8
D/OkHttp: Content-Length: 83
D/OkHttp: Connection: keep-alive
D/OkHttp: X-DNS-Prefetch-Control: off
D/OkHttp: X-Frame-Options: SAMEORIGIN
D/OkHttp: Strict-Transport-Security: max-age=15552000; includeSubDomains
D/OkHttp: X-Download-Options: noopen
D/OkHttp: X-Content-Type-Options: nosniff
D/OkHttp: X-XSS-Protection: 1; mode=block
D/OkHttp: Access-Control-Allow-Origin: *
D/OkHttp: ETag: W/"53-ocmggiYI6s3nDDtb9FHiqlrJJYU"
D/OkHttp: {"code":"unauthenticated","message":"Server Error. Try again later!!","status":401}
D/OkHttp: <-- END HTTP (83-byte body)
PS: I have also used Cookies but it didn't help
PS2: it says try again later, but it works perfectly fine with Postman
PS3: For some reasons, Volley works just fine :|
So I've found out what was the problem.
The problem was I had to use "https" and I was using http, apparently Volley and Postman both automatically add the s in the end but Retrofit didn't for some reasons.

Retrofit - Download csv file from server in android

I am trying to download a file from server using retrofit. Using HttpLoggingInterceptor I tried logging what is happening. I can find the file name. But the response body is empty.
I am new to using retrofit. can some one point me out in the right direction on where I am going wrong
Retrofit Client Interface:
public interface DownloadTransactions {
#Streaming
#GET("common/frontend/member/download.aspx")
Call<Void> getBalancesAndTransactions(#Header("Cookie") String header);
}
Java call:
void downloadData() {
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(15, TimeUnit.SECONDS);
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
client.addInterceptor(logging);
}
Retrofit.Builder builder = new Retrofit.Builder();
Retrofit retrofit = builder.client(client.build())
.baseUrl("https://" + getString(R.string.root_url))
.addConverterFactory(ScalarsConverterFactory.create())
.build();
DownloadTransactions downloadTransactions = retrofit.create(DownloadTransactions.class);
Call<Void> call = downloadTransactions.getBalancesAndTransactions(CommsManager.getInstance().getASPSessionId());
call.enqueue(new Callback<Void>() {
#Override
public void onResponse(Call<Void> call, retrofit2.Response<Void> response) {
Log.v(TAG, "success transactions: --------" + response);
}
#Override
public void onFailure(Call<Void> call, Throwable t) {
Log.v(TAG, "failure transactions: --------");
}
});
}
Response in log:
<-- 200 OK https://xxxxx.com/common/xxx/member/download.aspx (387ms)
D/OkHttp: Cache-Control: private
D/OkHttp: Content-Type: text/csv; charset=utf-16
D/OkHttp: Server: Microsoft-IIS/8.5
D/OkHttp: Content-Disposition: attachment; filename=Account_Summary_8978_04062018_142921.csv
D/OkHttp: X-AspNet-Version: 4.0.30319
D/OkHttp: X-Powered-By: ASP.NET
D/OkHttp: Date: Mon, 04 Jun 2018 13:29:21 GMT
D/OkHttp: Connection: keep-alive
D/OkHttp: Content-Length: 446900
D/OkHttp: Set-Cookie: xxx; path=/; Httponly; Secure
D/OkHttp: <-- END HTTP (binary 446900-byte body omitted)
D/Accounts Overview: success transactions: --------
Response{protocol=http/1.1, code=200, message=OK, url=xxx}
But the response body is empty.
Retrofit determines the type of response with the return type of the methods defined in your API interface.
The return type of getBalancesAndTransactionsis Void which means nothing.
Solution : Define your customise POJO class and replace Void with `POJO
or Just for testing purpose, you can use Object which can handle any type of response but you will have to use casting for specific operation
e.g
#Streaming
#GET("common/frontend/member/download.aspx")
Call<Object> getBalancesAndTransactions(#Header("Cookie") String header);
References :
How can we handle different response type with Retrofit 2?
Using Retrofit in Android

Retrofit 2 prints empty response body in log with HttpLoggingInterceptor.Level.BODY

I use Retrofit 2.0.0. This is the code that builds my HTTP client
protected OkHttpClient getHttpClient(){
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(
chain -> {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder();
requestBuilder.header("Accept-Language", App.getInstance().getPrefs().getSelectedLanguage().toLowerCase());
requestBuilder.header("Accept-Encoding", "gzip");
if(API.this instanceof PrivateAPI){
LoginResponse loginResponse = AuthManager.getInstanse().getLoginResponse();
requestBuilder.header("Authorization", String.format("%s %s",
loginResponse.getTokenType(),
loginResponse.getAccessToken()));
}
requestBuilder.method(original.method(), original.body());
Request request = requestBuilder.build();
return chain.proceed(request);
});
if(BuildConfig.DEBUG){
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(loggingInterceptor);
}
return builder.build();
}
And here is how I use it to init my Retrofit API interface:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(getGson()))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(getHttpClient())
.build();
apiService = retrofit.create(PrivateAPIInterface.class);
Until yesterday I was getting full log in logcat as it supposed to be. But suddenly I begin to get empty response bodies in my log. So here is an example of log I get now:
D/OkHttp: --> GET http://MY_DOMAIN/utility/gaz?branchid=&phone=21919&customerid= HTTP/1.1
D/OkHttp: Accept-Language: hy
D/OkHttp: Accept-Encoding: gzip
D/OkHttp: Authorization: 0yXK65Go_hRPT32WIP4DAKjmzIaM0riSLlybHhusvwmYJSxTdTVBrkL5CaqopXgrOuNFL5xvR5uVwCbprWrxvfYTMJ1I_AUmd5TKdNL4ptkUrJuOy1kGiule_yveCFv0GbTiAXhYjdfynJICJEVWc54ibnIv_Q14dLpPuVle5IaRuHmza2Pavkrhzi42sfMsK0Qpxaueu8eRPysk6MP9WkTnSKYIAThuq3sHq8RGAnkaLBx
D/OkHttp: --> END GET
D/OkHttp: <-- 200 OK http://MY_DOMAIN/utility/gaz?branchid=&phone=21919&customerid= (394ms)
D/OkHttp: Content-Length: 8527
D/OkHttp: Content-Type: application/json; charset=utf-8
D/OkHttp: Server: Microsoft-IIS/7.5
D/OkHttp: X-Powered-By: ASP.NET
D/OkHttp: Date: Wed, 16 Mar 2016 12:30:37 GMT
D/OkHttp: OkHttp-Sent-Millis: 1458131438086
D/OkHttp: OkHttp-Received-Millis: 1458131438438
D/OkHttp: }
D/OkHttp: <-- END HTTP (8527-byte body)
As you can see everything I get instead of response body is D/OkHttp: }.
But in Postman I see a normal response body and in my app everything works fine so the problem is only with logging.
Can anybody help me to understand why this can start happening?
P.S. In case of requests that has body I see the request body the problem is only with response bodys.
With retrofit-2 you are supposed to use OkHttp3 for getting full logs. Add this to your dependecies: compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://example.com")
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit.create(ApiClient.class);
ResClient.class
public class RestClient {
private static ApiInterface apiInterface;
static {
setupRestClient();
}
private static void setupRestClient() {
OkHttpClient httpClient = new OkHttpClient();
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(NetworkConstants.URL_BASE_LIVE)
.setLogLevel(RestAdapter.LogLevel.FULL)
.setLog(new AndroidLog("Retrofit"))
.setClient(new OkClient(httpClient))
.build();
apiInterface = restAdapter.create(ApiInterface.class);
}
public static ApiInterface getAdapter(){
return apiInterface;
}
}
ApiInterface.class
public interface ApiInterface {
// result api
//All result api async
#Headers({"Authorization: ######################"}) //for the header authorization if needed.
#GET(NetworkConstants.URL_GET_RESULTS)
void getResult(#QueryMap Map <String, String> params, Callback<ArrayList<Results>> callback);
}
Now call it like
// Fetching result through Api Hit
RestClient.getAdapter().getResult(getJobsParams(), new Callback<ArrayList<Result>>() {
#Override
public void success(ArrayList<Result> result, retrofit.client.Response response) {
// Do your Work
}
#Override
public void failure(RetrofitError error) {
// show server or other error to user
}
});

Categories

Resources