Accessing body string of an OkHttp Response twice results in IllegalStateException: closed

Update: As mugwort points out there is a simpler, more lightweight API available now (see GitHub issue): String responseBodyString = response.peekBody(Long.MAX_VALUE).string(); Log.d(“TAG”, responseBodyString); Original Answer: For explanation of the issue see Greg Ennis’ answer. However, if you can not easily pass the result in a variable, but still need to access the response body twice … Read more

How to specify a default user agent for okhttp 2.x requests

You can use an interceptor to add the User-Agent header to all your requests. For more information about okHttp interceptors see http://square.github.io/okhttp/interceptors/ Example implementation of this interceptor: /* This interceptor adds a custom User-Agent. */ public class UserAgentInterceptor implements Interceptor { private final String userAgent; public UserAgentInterceptor(String userAgent) { this.userAgent = userAgent; } @Override public … Read more

Retrofit 2: Catch connection timeout exception

For Retrofit 2 Define a listener in your web service instance: public interface OnConnectionTimeoutListener { void onConnectionTimeout(); } Add an interceptor to your web service: public WebServiceClient() { OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(10, TimeUnit.SECONDS); client.setReadTimeout(30, TimeUnit.SECONDS); client.interceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { return onOnIntercept(chain); } }); Retrofit retrofit = … Read more

CertPathValidatorException : Trust anchor for certificate path not found – Retrofit Android

DISCLAIMER: this answer is from Jul 2015 and uses Retrofit and OkHttp from that time. Check this link for more info on Retrofit v2 and this one for the current OkHttp methods. Okay, I got it working using Android Developers guide. Just as OP, I’m trying to use Retrofit and OkHttp to connect to a … Read more

How does OkHttp get Json string?

try { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(urls[0]) .build(); Response responses = null; try { responses = client.newCall(request).execute(); } catch (IOException e) { e.printStackTrace(); } String jsonData = responses.body().string(); JSONObject Jobject = new JSONObject(jsonData); JSONArray Jarray = Jobject.getJSONArray(“employees”); for (int i = 0; i < Jarray.length(); i++) { JSONObject object … Read more

javax.net.ssl.SSLException: Read error: ssl=0x9524b800: I/O error during system call, Connection reset by peer

Recently I faced the issue while working on some legacy code. After googling I found that the issue is everywhere but without any concrete resolution. I worked on various parts of the exception message and analyzed below. Analysis: SSLException: exception happened with the SSL (Secure Socket Layer), which is implemented in javax.net.ssl package of the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)