我用Android Kotlin提出了一个GET请求。下面的代码是一个基本的起点。响应返回"302找到“重定向,代码在响应头中获取Location。
由于R‘’Js的帮助,我从volley开始,在研究了下面的评论中提到的替代方案之后,转向了https://square.github.io/okhttp/。
在build.gradle中
implementation 'com.squareup.okhttp3:okhttp:4.9.0'网络部分现在起作用了。异步/回调似乎是必要的,因为执行开始于MainActivity (如https://square.github.io/okhttp/recipes/#asynchronous-get-kt-java中所述):
现在,我希望返回标头值,以便在正确的范围内以某种方式更新TextView。也许我需要
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
}
}
})
}
}发布于 2020-10-26 21:17:32
我想通了。与iOS不同,您可以在回调线程中直接更新TextView对象。
工作代码:
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
}
}
})
}
}https://stackoverflow.com/questions/64517058
复制相似问题