title: Android/Kotlin - okhttp4 GET request - return response headers and not follow redirect
body: <p>I'm making a GET request in Android Kotlin. The code below is a basic starting point. The response returns a &quot;302 Found&quot; redirect, and the code gets <code>Location</code> in the response headers.</p>
  <p>I started with <code>volley</code> and have switched to <a href="https://square.github.io/okhttp/" rel="nofollow noreferrer">https://square.github.io/okhttp/</a> after investigating the alternatives mentioned in below comments, thanks to R'Js help.</p>
  <p>In <code>build.gradle</code>:</p>
  <pre><code>    implementation 'com.squareup.okhttp3:okhttp:4.9.0'
  </code></pre>
  <p>The networking part is now working. The async / callback appears necessary, as execution begins in MainActivity (as described in <a href="https://square.github.io/okhttp/recipes/#asynchronous-get-kt-java" rel="nofollow noreferrer">https://square.github.io/okhttp/recipes/#asynchronous-get-kt-java</a>):</p>
  <p>I now want to get the header value back to update the <code>TextView</code> somehow in the right scope. Perhaps I need to</p>
  <ul>
  <li>create another callback to update this, or</li>
  <li>pass in a handle to the TextView and do the work inside the existing callback</li>
  <li>something else?</li>
  </ul>
  <pre class="lang-kotlin prettyprint-override"><code>package com.example.testlinks
  
  // Example deep linking app
  
  import android.content.Intent
  import android.net.Uri
  import android.os.Bundle
  import android.util.Log
  import android.widget.TextView
  import androidx.appcompat.app.AppCompatActivity
  import okhttp3.*
  import java.io.IOException
  
  class MainActivity : AppCompatActivity() {
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
  
          handleIntent(intent)
      }
  
      override fun onNewIntent(intent: Intent) {
          super.onNewIntent(intent)
          Log.i(&quot;MainActivity&quot;, &quot;onNewIntent called&quot;)
          handleIntent(intent)
      }
  
      private fun handleIntent(intent: Intent) {
          Log.i(&quot;MainActivity&quot;, &quot;handleIntent called&quot;)
  
          val appLinkAction = intent.action
          val appLinkData: Uri? = intent.data
          if (Intent.ACTION_VIEW == appLinkAction) {
              // handle URL
              val res : TextView = findViewById(R.id.result)
              res.text = appLinkData.toString()
  
              // Show this on our simple example app
              val location = makeRequest(appLinkData)
              val originalUrl : TextView = findViewById(R.id.originalURL)
  
          }
      }
  
      // Basic URL GET request.
      // See https://guides.codepath.com/android/Using-OkHttp
      //     https://square.github.io/okhttp/recipes/
  
      private fun makeRequest(url : Uri?) {
          // More efficient click-tracking with HTTP GET to obtain the &quot;302&quot; response, but not follow the redirect through to the Location.
          val client = OkHttpClient.Builder()
              .followRedirects(false)
              .build()
  
          val request = Request.Builder()
              .url(url.toString())
              .build()
  
          client.newCall(request).enqueue(object : Callback {
              override fun onFailure(call: Call, e: IOException) {
                  e.printStackTrace()
              }
  
              override fun onResponse(call: Call, response: Response) {
                  val originalURL = response.headers[&quot;Location&quot;]
                  if (originalURL != null) {
                      println(originalURL) // TODO: pass value back to calling function
                  }
              }
          })
      }
  }
  </code></pre>