okhttp

java.net.ProtocolException: unexpected end of stream

廉价感情. 提交于 2019-12-04 02:15:29
I am facing a strange issue, and I am not able to debug it out. I have implemented a logic for uploading stream of data and am using Volley for the same, I have customized a logic little bit in HurlStack , addBodyIfExists api,so that body of type "application/octet-stream" can be handled. My logic is to post progress to user, so that UI can be updated indicating user progress in upload, below my logic for same. int toRead = length; // File length byte[] data = new byte[4096]; connection.setDoOutput(true); if(length != -1) { connection.setFixedLengthStreamingMode(length); } else { connection

retrofit + okhttp : Retrieve GZIPInputStream

馋奶兔 提交于 2019-12-04 01:00:00
I have a problem when i activate gzip on WS using retrofit 1.4.1 and okhttp 1.3.0. RequestInterceptor requestInterceptor = new RequestInterceptor() { @Override public void intercept(RequestFacade request) { request.addHeader("content-type", "application/json"); request.addHeader("accept-encoding", "gzip"); // Here is the problem } }; RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint(Constants.HOST) .setLogLevel(RestAdapter.LogLevel.FULL) .setRequestInterceptor(requestInterceptor) .build(); If I comment the following line request.addHeader("accept-encoding", "gzip"); there is no

Retrofit - Too many follow-up requests: 21

女生的网名这么多〃 提交于 2019-12-04 00:51:47
I'm using retrofit to make requests. I've got following error: java.net.ProtocolException: Too many follow-up requests: 21 The code is like below: private OkHttpClient httpClient; private CookieManager cookieManager; public <S> S createCookieService(Class<S> serviceClass) { httpClient.interceptors().clear(); httpClient.setCookieHandler(cookieManager); Retrofit.Builder builder = new Retrofit .Builder() .client(httpClient) .baseUrl(url) .addConverterFactory(GsonConverterFactory.create()); Retrofit retrofit = builder.client(httpClient).build(); return retrofit.create(serviceClass); } And then I'm

How to use http/2 with Okhttp on Android devices?

流过昼夜 提交于 2019-12-04 00:46:07
I'M testing a site that supports HTTP/2, like this , and I try to use okhttp to send the request: OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder() .url("https://www.google.it") .build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { e.printStackTrace(); } @Override public void onResponse(Response response) throws IOException { Log.d("TestHttp", "OkHttp-Selected-Protocol: " + response.header("OkHttp-Selected-Protocol")); Log.d("TestHttp", "Response code is " + response.code()); } })

retrofit 2.0b2 : How to get InputStream from the response?

扶醉桌前 提交于 2019-12-04 00:18:02
I'm using Retrofit 2.0b2. After getting a response, I tried getting an InputStream from the response by : Response<JsonNode> response = call.execute(); InputStream is = response.raw().body().byteStream(); but the app keep throwing : java.lang.IllegalStateException: Cannot read raw response body of a converted body. at retrofit.OkHttpCall$NoContentResponseBody.source(OkHttpCall.java:184) at com.squareup.okhttp.ResponseBody.byteStream(ResponseBody.java:43) at ... Despite the response returned correctly. What am I doing wrong here ? iagreen If you want the raw stream, tell retrofit to return an

How to access context in a Interceptor?

扶醉桌前 提交于 2019-12-03 23:37:15
I would like to save some stuff on the SharedPreferences while being on a Interceptor. I can't find a way to do it because i can't find a way to access the context on the Interceptor (so not possible to use PreferencesManager, etc). public class CookieInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { PreferenceManager.getDefaultSharedPreferences(Context ??) }} Any ideas ? I'm using AndroidStudio & last version of OkHttp. Thanks ;) You can create a class that allows you to retrieve the context from anywhere (i.e. your interceptor): public

How do you prevent Retrofit from automatically following a 302

二次信任 提交于 2019-12-03 23:17:19
I have an authentication call that i'm trying to make using Retrofit on Android. The call returns a 302 to either a success or failure page. The original 302 response brings back a session cookie needed to maintain authentication on success, however Retrofit is automatically handing the request off to the redirect url before I get a chance to consume the cookie. Is there a way to prevent following the redirect? Or is there a way to write a response handler on Retrofit that can add the appropriate header before making the second call? Climbatize to prevent the redirect you have to configure

Picasso not loading pictures in ImageView

谁说我不能喝 提交于 2019-12-03 21:48:58
I'm trying to use Picasso in order to make a lazy picture loading in a ListView. But it's actually not working. In my custom adapter, I get the ImageView, initialize a Picasso object and indicate to load the image into the specified ImageView. To retrieve picture from server, I needed to provide a basic authentication, so I've created a custom interceptor which adds the authentication header. Moreover, I need to trust every SSL certificate because for the moment the certificate is not signed. The behaviors that I faced: Authentication error --> I've added the header. The error disappears. SSL

How to handle pagination in retrofit

最后都变了- 提交于 2019-12-03 17:17:44
问题 I'm building an app using retrofit. Everything's working swimmingly, but I'm worried about the size of my API requests and would like to split them up using pagination. What would be the best strategy to page through the API automatically with Retrofit, so that all available data is downloaded by default? 回答1: First, the pagination would need to be supported by the backend service that you're using. Second if you're looking to get an example of how this can be implemented from the client side

Retrofit add header with token and id

我怕爱的太早我们不能终老 提交于 2019-12-03 17:03:37
I have a problem with getting authenticated user. Before it I got token and user id. Now i need to get user from server using access token and id. I have header format Now I'am trying to add header with user token and id using interceptor. My code: Interceptor interceptor = new Interceptor() { @Override public okhttp3.Response intercept(Chain chain) throws IOException { Request newRequest = chain.request().newBuilder() .addHeader("Accept", "application/json") .addHeader("authorization", token) <-?? .addHeader("driver_id", id) <-?? .build(); return chain.proceed(newRequest); } }; OkHttpClient