1

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?

enter image description here enter image description here

  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
    }
3
  • 1
    "the long url after 200 code" -- what URL are you referring to? The only 200 that I see in your screenshots is in the line <-- 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. Commented Sep 2, 2024 at 18:35
  • Sorry, I meant this url: accounts.google.com/v3/signin. Do you know how to get it and save it to String? Commented Sep 2, 2024 at 20:42
  • 1
    You should already have it. It should be in your Retrofit definition for 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. Commented Sep 2, 2024 at 20:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.