0

I am deployed my rails server on EC2 instance and I am trying to get data from server using http requests (for that I am using okHttp. But I am facing some connection issue in some devices. I tried finding out the issue but still unable to do it.

kotlin code:

val client = OkHttpClient()
val body = FormBody.Builder().build()

// to create url I am using server IP directly without any port
val urlNetworkCheck = Constants.URL_CHECK_NETWORK_CONNECTION // http://10.40.21.16/api/v1/check_network
val requestNetworkCheck = Request.Builder().url(urlNetworkCheck).post(body).build()

client.newCall(requestNetworkCheck).enqueue(object: Callback {
    override fun onResponse(call: Call, response: Response) {
         val responseNetworkCheck = response.body?.string()
         logger.info { "Network connection is present." }
    }

    override fun onFailure(call: Call, e: IOException) {
        logger.info { "Could not connect to server. Exception: " + e.message }
        runOnUiThread {
            showToast(Constants.TOAST_NETWORK_ERROR)
        }
    }
})

On many devices, TOAST_NETWORK_ERROR is being shown. Please let me know what could be wrong here.

Update: Exception: CLEARTEXT communication to 18.224.2.107 not permitted by network security policy is what I am getting.

Sumit Jangra
  • 127
  • 1
  • 15

1 Answers1

1

It looks like Android P doesn't allow non-HTTP connections by default.

If your server doesn't support HTTPs, seems like you can enable it specifically according to this article: http://www.douevencode.com/articles/2018-07/cleartext-communication-not-permitted/

Or even this answer: https://stackoverflow.com/a/50834600/976880

aBadAssCowboy
  • 2,440
  • 2
  • 23
  • 38