I have @GET endpoint in response to which I receive an html code with URLs. I need to reach the URL that comes after 200 code. Do you have any idea how to do it in Android? I already tried to use the following code (used Jsoup) but can get url (short one) that comes after <!doctype html>. But cannot reach any info above especially the long url after 200 code. Could you please help me?
override suspend fun logInViaGoogle(): String? {
return try {
withContext(Dispatchers.IO) {
val response = apiService.requestGoogleAuthWindow()
if (response.isSuccessful) {
val htmlContent = response.body()?.string() ?: return@withContext null
Log.d("MyOwnUrlHtml", "html: $htmlContent")
// Parse the HTML document
val document: Document = Jsoup.parse(htmlContent)
Log.d("MyOwnUrlDoc", "document: $document")
// Extract the URL after "200"
val url: String? = extractUrlAfter200(document)
// Log the extracted URL
Log.d("MyOwnUrl", url ?: "URL is null")
url
} else {
// Handle unsuccessful response
null
}
}
} catch (e: retrofit2.HttpException) {
// Handle HTTP exception
throw e
} catch (e: Exception) {
// Handle other exceptions
null
}
}
fun extractUrlAfter200(document: Document): String? {
// Convert the document to a string
val documentText = document.text()
// Define the regex pattern to find the URL after "200"
val pattern = "200 (https?://\\S+)".toRegex()
// Search for the pattern in the document text
val matchResult = pattern.find(documentText)
// If a match is found, return the captured group (i.e., the URL)
return matchResult?.groups?.get(1)?.value
}


<-- 200 https://accounts.google.com/v3/signin. That is not a particularly "long" URL, and it is not in your HTML, but rather in what appears to be OkHttp logging.apiService.requestGoogleAuthWindow(). If the issue is that Google does a redirect and you need to get the final URL after the redirections, you could try this.