首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android/Kotlin - okhttp4 GET请求-返回响应头,而不是遵循重定向

Android/Kotlin - okhttp4 GET请求-返回响应头,而不是遵循重定向
EN

Stack Overflow用户
提问于 2020-10-24 19:08:18
回答 1查看 1.8K关注 0票数 0

我用Android Kotlin提出了一个GET请求。下面的代码是一个基本的起点。响应返回"302找到“重定向,代码在响应头中获取Location

由于R‘’Js的帮助,我从volley开始,在研究了下面的评论中提到的替代方案之后,转向了https://square.github.io/okhttp/

build.gradle

代码语言:javascript
复制
    implementation 'com.squareup.okhttp3:okhttp:4.9.0'

网络部分现在起作用了。异步/回调似乎是必要的,因为执行开始于MainActivity (如https://square.github.io/okhttp/recipes/#asynchronous-get-kt-java中所述):

现在,我希望返回标头值,以便在正确的范围内以某种方式更新TextView。也许我需要

  • 创建另一个回调来更新这个,或者
  • 向TextView传递一个句柄,并在现有回调中执行工作
  • 还有别的吗?
代码语言:javascript
复制
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("MainActivity", "onNewIntent called")
        handleIntent(intent)
    }

    private fun handleIntent(intent: Intent) {
        Log.i("MainActivity", "handleIntent called")

        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 "302" 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["Location"]
                if (originalURL != null) {
                    println(originalURL) // TODO: pass value back to calling function
                }
            }
        })
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-26 21:17:32

我想通了。与iOS不同,您可以在回调线程中直接更新TextView对象。

工作代码:

代码语言:javascript
复制
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("MainActivity", "onNewIntent called")
        handleIntent(intent)
    }

    private fun handleIntent(intent: Intent) {
        Log.i("MainActivity", "handleIntent called")

        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. The function updates the TextView itself
            makeRequest(appLinkData)
        }
    }

    // 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 "302" 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 locationURL = response.headers["Location"]
                if (locationURL != null) {
                    Log.i("locationURL", locationURL)
                    // Show this on our simple example app
                    val originalUrl : TextView = findViewById(R.id.originalURL)
                    originalUrl.text = locationURL
                }
            }
        })
    }
}

app运行的.gif

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64517058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档