HttpUrlConnection.connect fails

I've been trying to create a small app that connects to url in editText and returns the status code.

Here is the onCreate code:

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    button.setOnClickListener {
        val url = editText.text.toString()
        var connection: HttpURLConnection? = null
        if (URLUtil.isValidUrl(url)) {
            connection = URL(url).openConnection() as HttpURLConnection
            var thr = Thread {
                connection.connect()
                val code = connection.responseCode
                runOnUiThread {
                    if (code != 404 && code != 500) {
                        textView.text = "Ok"
                    } else {
                        textView.text = "Failed"
                    }
                }

            }
            thr.start()
            thr.join()
        } else {
            textView.text = "Failed"
        }
    }

The problem is that whenever URL is valid, runtime error occurs in .connect() method. Internet permission is set up in android manifest. What causes that issue?

E/AndroidRuntime: FATAL EXCEPTION: Thread-2
Process: com.example.company.myapplication, PID: 27183
java.net.SocketException: socket failed: EPERM (Operation not permitted)
    at java.net.Socket.createImpl(Socket.java:492)
    at java.net.Socket.getImpl(Socket.java:552)
    at java.net.Socket.setSoTimeout(Socket.java:1180)
    at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:143)
    at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:116)
    at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:186)
    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.connect(HttpURLConnectionImpl.java:131)
    at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:90)
    at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:30)
    at com.example.company.myapplication.MainActivity$onCreate$1$thr$1.run(MainActivity.kt:25)
    at java.lang.Thread.run(Thread.java:919)

2 answers

  • answered 2020-02-16 17:56 Chetan Garg

    Hi please check if you have provided the permission of internet in your manifest,

    <manifest >
     <uses-permission android:name="android.permission.INTERNET" />
     <application> 
      ...
     </application> 
    </manifest>
    

  • answered 2020-02-16 18:24 PiRocks

    For those who didn't read chat:

    URLUtil.isValidUrl(url) doesn't check that the url in question actually can be resolved to a website/server. This means that when connect is called on a website/server that doesn't exist, an exception is thrown, since there is nothing to connect to.