0

Lets say i have a url and i try to get the redirected url. I used the following code:

fun getRedirectUrl(url: String?): String? {
        var urlTmp: URL? = null
        var redUrl: String? = null
        var connection: HttpURLConnection? = null
        try {
            urlTmp = URL(url)
        } catch (e1: MalformedURLException) {
            e1.printStackTrace()
        }
        try {
            connection = urlTmp!!.openConnection() as HttpURLConnection
        } catch (e: IOException) {
            e.printStackTrace()
        }
        try {
            connection!!.responseCode
        } catch (e: IOException) {
            e.printStackTrace()
        }
        redUrl = connection!!.url.toString()
        connection.disconnect()
        return redUrl
    }

Even if in java compiler seems to work i get following error in android (copy paste from logcat).

 android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
        at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:115)
        at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
        at java.net.InetAddress.getAllByName(InetAddress.java:1152)
        at com.android.okhttp.Dns$1.lookup(Dns.java:41)
        at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:178)
        at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:144)
        at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:86)
        at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:176)
        at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
        at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:106)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:30)

also i am aware of this post. Why this happends? Any ideas?

mpountou
  • 798
  • 8
  • 15

1 Answers1

0

The problem was that i shouldnt run such code on the main thread. Using AsyncTask solved the problem. Example

private class getRedirectUrl(url:String) : AsyncTask<String?, Int?, String>() {
        val url = url
        override fun doInBackground(vararg p0: String?): String {
            var urlTmp: URL? = null
            var redUrl: String? = null
            var connection: HttpURLConnection? = null
            try {
                urlTmp = URL(url)
            } catch (e1: MalformedURLException) {
                e1.printStackTrace()
            }
            try {
                connection = urlTmp!!.openConnection() as HttpURLConnection
            } catch (e: IOException) {
                e.printStackTrace()
            }
            try {
                connection!!.responseCode
            } catch (e: IOException) {
                e.printStackTrace()
            }
            redUrl = connection!!.url.toString()
            connection.disconnect()
            return redUrl
        }

        override fun onPostExecute(result: String?) {
            super.onPostExecute(result)
            Log.d("Redirected URL",""+result)
        }
    }

now you can get the redirected url using

  getRedirectUrl("the url you want to get redirect").execute()
mpountou
  • 798
  • 8
  • 15