title: Handle redirects using Okhttp in Android
body: <p>I make some <strong>http requests using Okhttp to download a JSON</strong> from my server.
  For me and all my alpha testers everything works fine, but out in the wild I 
  experience some exceptions that occur, when I try to parse the response using 
  Gson. </p>
  
  <p>As far as I understand the <strong>exceptions occur, because the connection gets redirected</strong>, in situations where the user is at an airport or using a limited
  WIFI connection in an hotel etc.</p>
  
  <p>The exception:</p>
  
  <pre><code>Exception java.lang.IllegalStateException: closed
  okhttp3.internal.http.Http1xStream$FixedLengthSource.read (Http1xStream.java:378)
  okio.Buffer.writeAll (Buffer.java:956)
  okio.RealBufferedSource.readByteArray (RealBufferedSource.java:92)
  okhttp3.ResponseBody.bytes (ResponseBody.java:83)
  okhttp3.ResponseBody.string (ResponseBody.java:109)
  [xxxx].BackendFilter.handleResponse (BackendFilter.java:131)
  [xxxx].BackendFilter.access$300 (BackendFilter.java:30)
  [xxxx].BackendFilter$2.onResponse (BackendFilter.java:114)
  okhttp3.RealCall$AsyncCall.execute (RealCall.java:133)
  okhttp3.internal.NamedRunnable.run (NamedRunnable.java:32)
  java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1133)
  java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:607)
  java.lang.Thread.run (Thread.java:762)
  </code></pre>
  
  <p>From my logs  in firebase I can see that <strong>the URL of the response is different
  to the URL I used to set in the request</strong>.</p>
  
  <p>i.e. I get: <a href="http://www.tunisietelecom.tn/tt/themes/html/TT_Theme/infosolde.html" rel="noreferrer">http://www.tunisietelecom.tn/tt/themes/html/TT_Theme/infosolde.html</a></p>
  
  <p>It basically says: </p>
  
  <blockquote>
    <p>Dear Customer,</p>
    
    <p>Your plan is exhausted, to continue to benefit from your INTERNET
    offer, dial * 140 # for packages via your phone or * 145 # for Data
    key packages.</p>
  </blockquote>
  
  <p>So I assume this specific user got redirected, because his data package 
  got expired.</p>
  
  <p><strong>The response code nevertheless is a 200</strong> and my application tries to parse
  the response body, which fails obviously. Not with the excepetion I 
  expected, but it fails anyhow. <strong>(Can anybody explain to me, why this excepetion
  occurs. rather than a Gson <code>JsonSyntaxException</code>?)</strong></p>
  
  <p>I read multiple articles on stackoverflow and did a google search beforehand,
  but can anybody tell me <strong>how to handle this kind of redirects? 
  What is the best approach to smoothly dismiss this request, if I (how?) 
  recognize a redirect.</strong></p>
  
  <p><strong>tl;dr</strong></p>
  
  <p>My approach now would be to disable redirects in the first place by setting <code>OkHttpClient client = new OkHttpClient().newBuilder().followRedirects(false).followSslRedirects(false).build();</code>, but I am not sure (and familar with this topic), if there are 
  situations where redirects are total fine and I want to allow them.</p>
  
  <p>The next approach, without disabling redirects would be to investigate the 
  response. If the method <code>response.isRedirect()</code> returns true and the 
  url of the request (<code>response.request.url()</code>) is any different from
  my requested url, I would smoothly dismiss the request. 
  Is any of these approaches suitable? Are there better strategies to handle
  these kind of redirects? Does the exception really occur because of a
  redirect?</p>
  
  <p>Thank you all in advance!</p>