okhttp

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

How to pause/resume downloading with okhttp in Android

若如初见. 提交于 2019-12-03 02:47:27
I use okhttp library for download files in android. I download successfully. But something is wrong when I pause and resume download. Response request = new Request.Builder().url(url).build(); ResponseBody responseBody = response.body(); File file = new File(filePath); BufferedInputStream input = new BufferedInputStream(responseBody.byteStream()); OutputStream output; if (isResume) { output = new FileOutputStream(file, true); input.skip(downloadedSize); } else { output = new FileOutputStream(file, false); } long totalByteSize = responseBody.contentLength(); byte[] data = new byte[1024]; int

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

NetworkOnMainThreadException using OkHttp with RxJava2

最后都变了- 提交于 2019-12-02 23:11:35
问题 I am trying to learn Rxjava2. I am facing a problem i.e, NetworkOnMainThreadException during network call using okHttp. I have to use only okHttp libary. This is my method where I have written the code of RxJava2 for calling Login API. @Override public void onServerLoginClick(LoginRequest.ServerLoginRequest serverLoginRequest) { HttpParamObject httpParamObject = ApiGenerator.onServerLogin(serverLoginRequest); Service service = ServiceFactory.getInstance(activity, AppConstants.TASKCODES.LOGIN)

Glide - adding header to request

匆匆过客 提交于 2019-12-02 21:36:40
is there a method to add custom header to request when image is downloaded? I can use volley or okhttp in Glide. I try add cookie to cookiemanager in okhttpclient, but it doesn't helped. Is there a method to debug request response in Glide? Best regards Tom Since 3.6.0 it's possible to set custom headers for each request: GlideUrl glideUrl = new GlideUrl("url", new LazyHeaders.Builder() .addHeader("key1", "value") .addHeader("key2", new LazyHeaderFactory() { @Override public String buildHeader() { String expensiveAuthHeader = computeExpensiveAuthHeader(); return expensiveAuthHeader; } })

How to make https request with ssl certificate in Retrofit

随声附和 提交于 2019-12-02 21:04:44
I have a .p12 certificate file, and I use the SSL Converter to convert it to a .pem certificate file. Then I use that pem certificate file in my android code like this: OkHttpClient okHttpClient = new OkHttpClient(); try { CertificateFactory cf = CertificateFactory.getInstance("X.509"); InputStream instream = context.getResources().openRawResource(R.raw.pem_certificate); Certificate ca; ca = cf.generateCertificate(instream); KeyStore kStore = KeyStore.getInstance(KeyStore.getDefaultType()); kStore.load(null, null); kStore.setCertificateEntry("ca", ca); TrustManagerFactory tmf =

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:

How to change body in OkHttp Response?

安稳与你 提交于 2019-12-02 20:44:19
I'm using retrofit. To catch response i'm using Interceptor: OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.interceptors().add(myinterceptor); here is code of interceptor: new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Response response = chain.proceed(request); if (path.equals("/user")){ String stringJson = response.body().string(); JSONObject jsonObject = new JSONObject(stringJson); jsonObject.put("key",1); //here I need to set this new json to response and then return this response How to change body

How cancel task with retrofit and rxjava

不想你离开。 提交于 2019-12-02 20:37:56
I have rest api. @Get("/serveraction") public Observable<String> myRequest(@Query("Data") String data); I know, that okhttp has canceling functionality(by request object, by tag), but don't know how use it with retrofit and rxjava. What is the best way to realize canceling mechanism for network tasks with retrofit and rxjava? You can use the standard RxJava2 cancelling mechanism Disposable . Observable<String> o = retrofit.getObservable(..); Disposable d = o.subscribe(...); // later when not needed d.dispose(); Retrofit RxJava call adapter will redirect this to okHttp's cancel. RxJava1: https:

Intercept and retry call by means of OkHttp Interceptors

坚强是说给别人听的谎言 提交于 2019-12-02 20:13:13
I need to retry request inside of OkHttp Interceptor . For example there is incoming request which needs Authorization token. If Authorization token is expired, server returns response with 403 code. In this case I am retrieving a new token and trying to make call again by using the same chain object. But OkHttp throws an exception, which states that you cannot make two requests with the same chain object. java.lang.IllegalStateException: network interceptor org.app.api.modules.ApplicationApiHeaders@559da2 must call proceed() exactly once I wonder if there is a clean solution to this problem