I'm trying to cache the response of http calls done by Retrofit(v 1.9.0) with OkHttp(2.3.0). It always made the network calls if I try to make a call without internet then java.net.UnknownHostException.
RestClient
public class RestClient {
public static final String BASE_URL = "http://something.example.net/JSONService";
private com.ucc.application.rest.ApiService apiService;
public RestClient() {
Gson gson = new GsonBuilder()
.setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'")
.create();
RequestInterceptor requestInterceptor = new RequestInterceptor() {
#Override
public void intercept(RequestFacade request) {
request.addHeader("Accept", "application/json");
int maxAge = 60 * 60;
request.addHeader("Cache-Control", "public, max-age=" + maxAge);
}
};
RestAdapter restAdapter = new RestAdapter.Builder()
.setLogLevel(RestAdapter.LogLevel.FULL)
.setEndpoint(BASE_URL)
.setClient(new OkClient(OkHttpSingleTonClass.getOkHttpClient()))
.setConverter(new GsonConverter(gson))
.setRequestInterceptor(requestInterceptor)
.build();
apiService = restAdapter.create(com.ucc.application.rest.ApiService.class);
}
public com.ucc.application.rest.ApiService getApiService() {
return apiService;
}
}
OkHttpSingleTonClass
public class OkHttpSingleTonClass {
private static OkHttpClient okHttpClient;
private OkHttpSingleTonClass() {
}
public static OkHttpClient getOkHttpClient() {
if (okHttpClient == null) {
okHttpClient = new OkHttpClient();
createCacheForOkHTTP();
}
return okHttpClient;
}
private static void createCacheForOkHTTP() {
Cache cache = null;
cache = new Cache(getDirectory(), 1024 * 1024 * 10);
okHttpClient.setCache(cache);
}
public static File getDirectory() {
final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "UCC" + File.separator);
root.mkdirs();
final String fname = UserUtil.CACHE_FILE_NAME;
final File sdImageMainDirectory = new File(root, fname);
return sdImageMainDirectory;
}
}
MyActivity
Request request = new Request.Builder()
.cacheControl(new CacheControl.Builder()
.onlyIfCached()
.maxAge(60 * 60, TimeUnit.SECONDS)
.build())
.url(RestClient.BASE_URL + Constants.GET_ABOUT_US_COLLECTION + "?userid=59e41b02-35ed-4962-8517-2668b5e8dae3&languageid=488d8f13-ef7d-4a3a-9516-0e0d24cbc720")
.build();
Log.d("url_request", RestClient.BASE_URL + Constants.GET_ABOUT_US_COLLECTION + "/?userid=10");
com.squareup.okhttp.Response forceCacheResponse = null;
try {
forceCacheResponse = OkHttpSingleTonClass.getOkHttpClient().newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
if (forceCacheResponse.code() != 504) {
// The resource was cached! Show it.
Log.d("From", "Local");
Toast.makeText(AboutUs.this, "Local", Toast.LENGTH_SHORT).show();
} else {
// The resource was not cached.
Log.d("From", "Network");
Toast.makeText(AboutUs.this, "Network", Toast.LENGTH_SHORT).show();
getAbouUsDetails();//This will use the Apiservice interface to hit the server.
}
I followed this. But I can't manage to work. Its simply hitting from the server. What am i doing wrong?
As per Retrofit 1.9.0 which uses OkClient does not have Caching support. We have to use OkHttpClient instance by Square OkHttpClient library.
You can compile by compile 'com.squareup.okhttp:okhttp:2.3.0'
Before everything retrofit caches by response headers like
Cache-Control:max-age=120,only-if-cached,max-stale
** 120 is seconds.
You can read more about headers here.
Caching headers are mostly instructed by server response. Try to implement cache headers in servers. If you don't have an option, yes retrofit has it.
private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder()
.header("Cache-Control", String.format("max-age=%d, only-if-cached, max-stale=%d", 120, 0))
.build();
}
};
Where to cache:
private static void createCacheForOkHTTP() {
Cache cache = null;
cache = new Cache(getDirectory(), 1024 * 1024 * 10);
okHttpClient.setCache(cache);
}
// returns the file to store cached details
private File getDirectory() {
return new File(“location”);
}
Add interceptor to the OkHttpClient instance:
okHttpClient.networkInterceptors().add(REWRITE_CACHE_CONTROL_INTERCEPTOR);
And finally add OkHttpClient to the RestAdapter:
RestAdapter.setClient(new OkClient(okHttpClient));
And you can go through this slide for more reference.
Related
I have an interceptor which works fine when i turn off internet it throw's the exception as expected but the problem occur when i turn on internet back again i get timeout exception.
AppModule.java
#Singleton
#Provides
Retrofit provideRetrofitInstance(RetrofitInterceptor retrofitInterceptor) {
return new Retrofit.Builder()
.baseUrl(BuildConfig.URL)
.client(new OkHttpClient()
.newBuilder()
.readTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.addInterceptor(retrofitInterceptor)
.build())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
#Provides
#Singleton
RetrofitInterceptor provideApiInterceptor(Context context) {
return new RetrofitInterceptor(context);
}
RetrofitInterceptor.java
public class RetrofitInterceptor implements Interceptor {
public static final String TAG = RetrofitInterceptor.class.getSimpleName();
private String sessionId;
private Context context;
#Inject
public RetrofitInterceptor(Context context) {
this.context = context;
}
#Override
public Response intercept(Chain chain) throws IOException {
if (NetworkUtils.isNetworkConnected(context)) {
Request request = chain.request();
Request.Builder requestBuilder = request.newBuilder();
if (sessionId != null) {
requestBuilder.header("sessionId", sessionId);
}
Response response = chain.proceed(requestBuilder.build());
return response;
} else {
throw new NoConnectivityException();
}
}
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
}
I do not want to use a generated SDK for my API Gateway endpoint. I want to use the plain AWS SDK. When I googled how to do that, nothing obvious came up in the first few hits (surprisingly). So I cobbled this together:
private static void invokeApiGatewayWithCognito(String identityPoolId, String endpoint) throws URISyntaxException {
AmazonCognitoIdentity cognitoClient = AmazonCognitoIdentityClient.builder()
.withCredentials(new AWSCredentialsProvider() {
#Override
public AWSCredentials getCredentials() {
return new AnonymousAWSCredentials();
}
#Override
public void refresh() {
}
}).withRegion(EU_CENTRAL_1.getName()).build();
String identityId = cognitoClient.getId(new GetIdRequest()
.withIdentityPoolId(identityPoolId))
.getIdentityId();
Credentials cognitoCreds = cognitoClient
.getCredentialsForIdentity(
new GetCredentialsForIdentityRequest()
.withIdentityId(identityId))
.getCredentials();
AWS4Signer signer = new AWS4Signer();
signer.setServiceName("execute-api");
signer.setRegionName(EU_CENTRAL_1.getName());
AmazonHttpClient amazonHttpClient =
new AmazonHttpClient(new ClientConfiguration());
Request r = new DefaultRequest("execute-api");
r.setEndpoint(new URI(endpoint));
r.setHttpMethod(HttpMethodName.GET);
BasicSessionCredentials basicCreds = new BasicSessionCredentials(
cognitoCreds.getAccessKeyId(),
cognitoCreds.getSecretKey(),
cognitoCreds.getSessionToken());
signer.sign(r, basicCreds);
amazonHttpClient
.requestExecutionBuilder()
.request(r)
.execute(logResponse());
}
private static HttpResponseHandler<Void> logResponse() {
return new HttpResponseHandler<Void>() {
#Override
public Void handle(HttpResponse httpResponse) throws Exception {
System.out.println("response code = " + httpResponse.getStatusCode());
System.out.println("response headers = " + httpResponse.getHeaders());
System.out.println("response content = " + new String(bytes(httpResponse.getContent())));
return null;
}
#Override
public boolean needsConnectionLeftOpen() {
return false;
}
};
}
It works, but boy is that ugly code. What am I missing? How can this be done more cleanly?
Hi From last two I'm stuck with this retrofit any one please help me.I have tried so many method to pass header in retrofit could but i couldn't im using Retrofit 2.0.1
build.gradle
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
compile 'com.squareup.okhttp:okhttp:2.7.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
And in
ApiClientHeader.jav
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ApiClientHeader {
public static final String BASE_URL = "URL";
private static Retrofit retrofit = null;
public static Retrofit getClient(final String token) {
OkHttpClient okClient = new OkHttpClient.Builder()
.addInterceptor(
new Interceptor() {
#Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", "Bearer " + token)
.method(original.method(), original.body());
Request request = requestBuilder.build();
return chain.proceed(request);
}
})
.build();
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
And In my
ApiInterface.java
#GET("profile")
Call<JsonObject> getProfile();
And In My Activity i am just calling function
private void getProfileData()
{
Singleton single = new Singleton();
String auth = single.getAuthorization();
Log.d("===========>>>>>>",auth);
ApiInterface apiService =
ApiClientHeader.getClient(auth).create(ApiInterface.class);
//showProgress(true);
Call<JsonObject> profileResponse = apiService.getProfile();
profileResponse.enqueue(new Callback<JsonObject>() {
#Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
//showProgress(false);
Log.d("============>"," Call Request " +String.valueOf(call.request().toString()));
Log.d("============>", " Response code " + String.valueOf(response.code()));
// Log.d("============>", " Response Body " + String.valueOf(response.body().toString()));
if(response.code() == HttpURLConnection.HTTP_OK)
{
}
}
#Override
public void onFailure(Call<JsonObject> call, Throwable t) {
//showProgress(false);
Log.d("============>"," Call Request " +String.valueOf(call.request().toString()));
Log.d("============>"," Call Request " +String.valueOf(call.request().headers()));
Log.d("=======>", "Failure" + t.getMessage());
}
});
}
Still I am getting 403 Invalid acces.
Here i have not used any POJO class to send or receive data. Please help me.
You have to pass your authentication string like this
#GET("profile")
Call<JsonObject> getProfile((#Header("Authorization") String authorization))
#Headers({
"Accept: application/vnd.github.v3.full+json",
"User-Agent: Retrofit-Sample-App"
})
#GET("users/{username}")
Call<User> getUser(#Path("username") String username);
more info on documentation
I found one of the best link for Token based authentication using Retrofit 1.9 + OkHttp 2.4
You can add NetworkInterceptor, I have used it in my demo like:
httpClient.addNetworkInterceptor(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
// Add authorization header with updated authorization value to intercepted request
Request authorisedRequest = originalRequest.newBuilder()
.header("Authorization", AccountAuthenticator.getAccessTokenWithTokenType(mContext))
.build();
return chain.proceed(authorisedRequest);
}
});
Dependencies:
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
Thank you.
Try below code with your class, It is working for me:
final RestAdapter restAdapter = new RestAdapter.Builder().setClient(client).setLogLevel(RestAdapter.LogLevel.FULL).setRequestInterceptor(new RequestInterceptor() {
#Override
public void intercept(RequestFacade requestFacade) {
requestFacade.addHeader("key", "value");
requestFacade.addHeader("X-Requested-With", "XMLHttpRequest");
}
}).setConverter(new GsonConverter(gson)).setEndpoint(context.getString(R.string.base_url)).build();
Edit Post :
public class RestClient {
// private static final String BASE_URL = "http://api.plumperfect.com";
private WebServicesInterface apiService;
private static RestClient instance;
public RestClient(Context context) {
instance = this;
final Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ItemTypeAdapterFactory()).setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'").create();
// final int cacheSize = 10 * 1024 * 1024; // 10 MiB
// final File cacheDirectory = new File(context.getCacheDir().getAbsolutePath(), "HttpCache");
// final OkHttpClient client = new OkHttpClient();
// Cache cache;
// try {
// cache = new Cache(cacheDirectory, cacheSize);
// client.setCache(cache);
// } catch (IOException e) {
// e.printStackTrace();
// }
final OkHttpClient okHttpClient = new OkHttpClient();
final Client client = new OkClient(okHttpClient);
final RestAdapter restAdapter = new RestAdapter.Builder().setClient(client).setLogLevel(RestAdapter.LogLevel.FULL).setRequestInterceptor(new RequestInterceptor() {
#Override
public void intercept(RequestFacade requestFacade) {
requestFacade.addHeader("key", "value");
requestFacade.addHeader("X-Requested-With", "XMLHttpRequest");
}
}).setConverter(new GsonConverter(gson)).setEndpoint(context.getString(R.string.base_url)).build();
apiService = restAdapter.create(WebServicesInterface.class);
}
public WebServicesInterface getApiService() {
return apiService;
}
public static RestClient getInstance() {
return instance;
}
}
Secound Class :
public class ItemTypeAdapterFactory implements TypeAdapterFactory {
#Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<T>() {
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
}
public T read(JsonReader in) throws IOException {
JsonElement jsonElement = elementAdapter.read(in);
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has(Constants.DATA) && jsonObject.get(Constants.DATA).isJsonObject()) {
jsonElement = jsonObject.get(Constants.DATA);
}
}
return delegate.fromJsonTree(jsonElement);
}
}.nullSafe();
}
}
Why wont this compile:
MyOkHttpClient okClient = new MyOkHttpClient.Builder()
.addInterceptor(new AddCookiesInterceptor())
.addInterceptor(new ReceivedCookiesInterceptor()).build();
Incompatible types.
Required:
my.path.util.auth.MyOkHttpClient
Found:
okhttp3.OkHttpClient
This is MY class:
public class MyOkHttpClient extends okhttp3.OkHttpClient implements Authenticator {
private static int MAX_AUTHENTICATE_TRIES = 3;
#Override
public Request authenticate(Route route, Response response) throws IOException {
if (responseCount(response) >= MAX_AUTHENTICATE_TRIES) {
return null; // If we've failed 3 times, give up. - in real life, never give up!!
}
String credential = Credentials.basic(AUTHTOKEN_USERNAME, AUTHTOKEN_PASSWORD);
return response.request().newBuilder().header("Authorization", credential).build();
}
private int responseCount(Response response) {
int result = 1;
while ((response = response.priorResponse()) != null) {
result++;
}
return result;
}
}
Based on your comments, you incorrectly believe that you are decorating OkHttpClient with custom authentication logic.
Instead, you are unnecessarily extending OkHttpClient and implementing the Authenticator interface. You can simply build the standard OkHttpClient with any custom authenticator you would like.
As such, this is more like what you actually want
public class MyAuthenticator implements Authenticator {
private static int MAX_AUTHENTICATE_TRIES = 3;
#Override
public Request authenticate(Route route, Response response) throws IOException {
if (responseCount(response) >= MAX_AUTHENTICATE_TRIES) {
return null; // If we've failed 3 times, give up. - in real life, never give up!!
}
String credential = Credentials.basic(AUTHTOKEN_USERNAME, AUTHTOKEN_PASSWORD);
return response.request().newBuilder().header("Authorization", credential).build();
}
private int responseCount(Response response) {
int result = 1;
while ((response = response.priorResponse()) != null) {
result++;
}
return result;
}
}
And then when you build your client
OkHttpClient okClient = new OkHttpClient.Builder()
.addInterceptor(new AddCookiesInterceptor())
.addInterceptor(new ReceivedCookiesInterceptor())
.authenticator(new MyAuthenticator())
.build();
Since 23 sdk Android class were excluded classes:
org.apache.http.auth.AuthScheme;
org.apache.http.auth.AuthSchemeFactory;
org.apache.http.impl.auth.NTLMScheme;
org.apache.http.impl.auth.NTLMEngine;
org.apache.http.impl.auth.NTLMEngineException;
As it is now authorized in AD, with login and password through a retrofit? There OKHttpklient can be through headers?
1) add package org.apache.httpcomponents:httpclient:4.5 to build.gradle (app)
//noinspection DuplicatePlatformClasses
implementation '**org.apache.httpcomponents:httpclient:4.5**'
2) Add package org.apache.http.impl.auth to your project (folder in /java)
3) Create public class in added org.apache.http.impl.auth package
public class PublicNTLMEngineImpl implements NTLMEngine {
// with content of http://svn.apache.org/repos/asf/httpcomponents/httpclient/tags/4.5.2/httpclient/src/main/java/org/apache/http/impl/auth/NTLMEngineImpl.java
}
4) use Giohji's NTLMAuthenticator with instance of new PublicNTLMEngineImpl
OkHttpClient httpClient = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.authenticator(new NTLMAuthenticator(username, password, domainOrComputerName))
.build();
5) source code:
http://svn.apache.org/repos/asf/httpcomponents/httpclient/tags/4.5.2/httpclient/src/main/java/org/apache/http/impl/auth/
I found the answer on okhttp's github.
It was posted by SelvinPL.
First you have to implement the NTLM authenticator (it uses NTLMEngineImpl, a standalone version of org.apache.http.impl.auth.NTLMEngineImpl, which was also created by SelvinPL). The code below is a slightly modified version of SelvinPL's implementation to run on the latest retrofit's version (2.1.0).
private static class NTLMAuthenticator implements Authenticator {
final NTLMEngineImpl engine = new NTLMEngineImpl();
private final String domain;
private final String username;
private final String password;
private final String ntlmMsg1;
private NTLMAuthenticator(String username, String password, String domain) {
this.domain = domain;
this.username = username;
this.password = password;
String localNtlmMsg1 = null;
try {
localNtlmMsg1 = engine.generateType1Msg(null, null);
} catch (Exception e) {
e.printStackTrace();
}
ntlmMsg1 = localNtlmMsg1;
}
#Override
public Request authenticate(Route route, Response response) throws IOException {
final List<String> WWWAuthenticate = response.headers().values("WWW-Authenticate");
if (WWWAuthenticate.contains("NTLM")) {
return response.request().newBuilder().header("Authorization", "NTLM " + ntlmMsg1).build();
}
String ntlmMsg3 = null;
try {
ntlmMsg3 = engine.generateType3Msg(username, password, domain, "android-device", WWWAuthenticate.get(0).substring(5));
} catch (Exception e) {
e.printStackTrace();
}
return response.request().newBuilder().header("Authorization", "NTLM " + ntlmMsg3).build();
}
}
Then you can register the authenticator like this example:
OkHttpClient client = new OkHttpClient.Builder()
.authenticator(new NTLMAuthenticator(username, password, domain))
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(getURL(context))
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit.create(Api.class);
It works for com.squareup.retrofit2:retrofit:2.1.0.