okhttp

Make a synchronous Retrofit call from inside an OkHttp Interceptor

江枫思渺然 提交于 2019-12-07 04:34:27
问题 I am trying to automatically refresh an auth token if it is expired. I am using the new Interceptor class that was introduced in OkHttp 2.2 . In the intercept method I am trying the original request with chain.proceed(request) , checking the response code, and if the token is expired I am making a call to a separate Retrofit service, synchronously, to obtain a new token. The strange thing is, no code past the synchronous call seems to run. If I try debugging with a breakpoint on the

Cache POST requests with OkHttp

你离开我真会死。 提交于 2019-12-07 03:02:31
问题 I make some POST requests to my server with OkHttp. This works fine. Now I want use the cache of OkHttp to cache the responses of the requests and use them when the device is offline. I tried many solutions from other questions, but none of them work. I use OkHttp 2.5.0 With the code below, I get a valid response, when the device has internet access. But if I turn the internet off, it throws a java.net. UnknownHostException : Unable to resolve host "example.com": No address associated with

Okhttp Authenticator multithreading

微笑、不失礼 提交于 2019-12-07 00:42:16
问题 I am using OkHttp in my android application with several async requests. All requests require a token to be sent with the header. Sometimes I need to refresh the token using a RefreshToken, so I decided to use OkHttp 's Authenticator class. What will happen when 2 or more async requests get a 401 response code from the server at the same time? Would the Authenticator's authenticate() method be called for each request, or it will only called once for the first request that got a 401? @Override

UnknownHostException: name or service not known

大城市里の小女人 提交于 2019-12-06 23:01:20
问题 I'm attempting to return some data from an API using OkHttpClient in com.squareup.okhttp. I've run into a few errors that i have eventually been able to overcome but i can't get past this host exception error and nothing on here seems to be specific enough to my case to be able to solve. Below is the code i have attempted along with the output with it, if anyone has any idea how to overcome the error i would appreciate it. CODE public void connect() { OkHttpClient client = new OkHttpClient();

Retrofit + OkHttp + GZIP-ed JSON

走远了吗. 提交于 2019-12-06 22:18:14
问题 I'm new to Retrofit but it seems really powerful. Everything works fine with a normal JSON, but as soon as I try a GZIPed one I just get an error: I/System.out(14883): ------retrofit.RetrofitError: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 Obviously the the gibberish symbols I see in the Log cannot be parsed as JSON. I thought okHttp is catchig that gzip stuff correctly?

How to cancel the request using OKHTTP

南笙酒味 提交于 2019-12-06 20:52:16
问题 I need to be able to MANAGER some requests with OKHTTP , using the Google Places AutoComplete to receive some predictions by typing the Address. The Problem is Each time that I insert a CHAR it will make a new request but at the same time I need to cancel the previous one! Ex : New York City = 13 Requests at the same time! So I'm using a single instance of Call trying to cancel anything that has been requested but with no success. This is what I did! Address.addTextChangedListener(new

okhttp client throwing exception under TMG proxy server

瘦欲@ 提交于 2019-12-06 16:00:19
问题 I'm using github's square OKHTTP client library for my java application as follows : import java.io.IOException; import java.net.InetSocketAddress; import java.net.Proxy; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import com.squareup.okhttp.Authenticator; import com

Volley post request not working

时光怂恿深爱的人放手 提交于 2019-12-06 16:00:00
问题 I am new to Volley and OkHttp and I am trying to login to a website using OkHttp as the transport layer for Volley . Login needs to be done through SSL and has several redirects implemented. OkHttp has been forced to not automatically follow redirects as I need to do this manually for this site (I think), but I am failing at the post request (I am not sure though). The post requests needs some params provided like this: source=AEM&target=IAM&URL=selfcare&login-form-type=pwd&username=user

OkHttp, Android - download a html page and display this content in a view

浪子不回头ぞ 提交于 2019-12-06 13:49:36
I would like download a html page in order to display some information in a TextView. Everytime, my program displays "error" in the TextView. Why doesn't my program work ? I add in the AndroidManifest.xml file, this line: <uses-permission android:name="android.permission.INTERNET" /> Here my activity_main.xml file: <?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical

OKHttp Authenticator custom http code other than 401 and 407

萝らか妹 提交于 2019-12-06 12:49:28
问题 I have oauth token implemented on server side but upon Invalid token or Token expirey i am getting 200 http status code but in response body i have {"code":"4XX", "data":{"some":"object"} When i try to read string in interceptor i get okhttp dispatcher java.lang.illegalstateexception closed because response.body().string() must be called only once. Also i read from here Refreshing OAuth token using Retrofit without modifying all calls that we can use OkHttp Authenticator class but it works