okhttp3

Detect if OkHttp response comes from cache (with Retrofit)

天大地大妈咪最大 提交于 2019-12-03 05:20:44
Is there a way to detect if a Retrofit response comes from the configured OkHttp cache or is a live response? Client definition: Cache cache = new Cache(getCacheDirectory(context), 1024 * 1024 * 10); OkHttpClient okHttpClient = new OkHttpClient.Builder() .cache(cache) .build(); Api definition: @GET("/object") Observable<Result<SomeObject>> getSomeObject(); Example call: RetroApi retroApi = new Retrofit.Builder() .client(okHttpClient) .baseUrl(baseUrl) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build() .create(RetroApi.class);

Upload dynamic number of files with okHttp3

若如初见. 提交于 2019-12-03 02:53:16
How to manage upload of dynamic number of files with OkHttp v3, I have already implemented with older version of OkHttp which was compile 'com.squareup.okhttp:okhttp:2.6.0' There are some changes in class Form and Multipart bodies are now modeled. They've replaced the opaque FormEncodingBuilder with the more powerful FormBody and FormBody.Builder combo. Similarly they've upgraded MultipartBuilder into MultipartBody, MultipartBody.Part, and MultipartBody.Builder. below code is of older version final MediaType MEDIA_TYPE = MediaType.parse(AppConstant.arrImages.get(i).getMediaType()); //If you

Android Retrofit2 Refresh Oauth 2 Token

老子叫甜甜 提交于 2019-12-03 01:58:36
问题 I am using Retrofit and OkHttp libraries. So I have Authenticator which authanticate user if gets 401 response. My build.gradle is like that : compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4' compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4' compile 'com.squareup.okhttp3:okhttp:3.1.2' And my custom Authenticator is here : import java.io.IOException; import okhttp3.Authenticator; import okhttp3.Request; import okhttp3.Response; import okhttp3.Route; public class

Retrofit 2 HTTP method annotation is required (e.g., @GET, @POST, etc.)

*爱你&永不变心* 提交于 2019-12-02 23:24:05
What's wrong with my Retrofit configuration? I'm having this error when I'm adding Basic Authentication with my OkHttpClient but when I used the default client without Interceptor it's working. Or is there something wrong with my Gradle Dependencies..? E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.IllegalArgumentException : HTTP method annotation is required (e.g., @GET, @POST, etc.). for method APIService.getRegAccrDetails at retrofit.Utils.methodError(Utils.java:177) at retrofit.Utils.methodError(Utils.java:167) at retrofit.RequestFactoryParser.parseMethodAnnotations(RequestFactoryParser

OkHttp3 Never Timeout on slow internet

☆樱花仙子☆ 提交于 2019-12-02 20:57:53
First of all, I have read so many questions regarding my question but it never gives me the solution. Here are some of the questions which I read regarding my question. Question 1 Question 2 Qusetion 3 Question 4 Question 5 Question 6 Question 7 I also read this article regarding my question but it also never provide me the solution. Problem: I am using Okhhtp3 library in my application for Web Services. It's working fine but when the internet connection slow or unreliable connection it's stuck and never times out or never calls the timeout exception or failure method. Here is the client code:

OkHttp 3 response cache (java.net.UnknownHostException)

可紊 提交于 2019-12-02 10:44:57
I'm really stuck at this problem. I want to cache server response for some time to use the data when device is offline. So I set a cache to my OkHttpClient . Also I set Cache-Contlol header. Here is my code: public class MainActivity extends AppCompatActivity { Retrofit retrofit; RecyclerView recyclerView; RandomUserAdapter mAdapter; Cache cache; Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); GsonBuilder gsonBuilder = new GsonBuilder(); Gson gson = gsonBuilder.create(); cache

Retrofit Offline cashing returns a null response.body()

你离开我真会死。 提交于 2019-12-02 02:58:37
I tried this link and this link to construct an offline Retrofit cache. The problem is that if I put the phone in Airplane mode, the Response.body() is always null. Here's my code: OkHttpClient client = new OkHttpClient .Builder() .cache(new Cache(App.sApp.getCacheDir(), 10 * 1024 * 1024)) // 10 MB .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); if (App.isNetworkAvailable()) { request = request.newBuilder().header("Cache-Control", "public, max-age=" + 60).build(); } else { request = request.newBuilder(

CompositeDisposable.clear causes OkHttp to throw java.lang.IllegalStateException: Unbalanced enter/exit

℡╲_俬逩灬. 提交于 2019-12-01 21:30:33
So I have a simple http request using OkHttp. I do this with RxJava on Android. I add this RxJava call to a CompositeDisposable that I then clear on onStop . For some reason that is triggering this exception below. I'm a little unsure about how to fix it. Caused by java.lang.IllegalStateException: Unbalanced enter/exit at okio.AsyncTimeout.enter(AsyncTimeout.java:73) at okio.AsyncTimeout$2.read(AsyncTimeout.java:235) at okio.RealBufferedSource.read(RealBufferedSource.java:47) at okhttp3.internal.http1.Http1Codec$AbstractSource.read(Http1Codec.java:363) at okhttp3.internal.http1.Http1Codec

What is the difference between the OkHttp methods .toString() and .string()?

若如初见. 提交于 2019-12-01 18:56:01
I have a snippet of code: override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) { try { Log.d("DEBUG POST=====>", response.body()!!.string()) }catch(e:IOException) { e.printStackTrace() } } When I use response.body()!!.string() I get the correct output, and JSON body. When I use: response.body().toString() I get okhttp3.ResponseBody$1@c626d25 Can anyone kindly tell me what is the difference between the two methods? Zoe the transgirl string() isn't a valid Kotlin (or Java) method, as in neither of the languages define it. It's defined by OkHttp in ResponseBody and

Retrofit Closing Response Body

别说谁变了你拦得住时间么 提交于 2019-12-01 17:40:35
I've been getting this error: A connection to ****** was leaked. Did you forget to close a response body? So I went on and close the responses I get. response.body().close() Problem is if the response.body() is already converted to a custom class, there's no close method available. Also I tried calling raw and gives me an exception: fetchSomething.enqueue(new Callback<SomethingClass>() { @Override public void onResponse(Call<SomethingClass> call, Response<SomethingClass> response) { //Closes the response body response.raw().body().close(); //<--- gives illegalStateException } @Override public