'How to convert apache httpclient code to okhttp

I am using api with oauth2 in my application. This code is provided by the api developers, but it was written for java and, as I understand it, httpclient is not supported on android. Alternatively, I think okhttp can be used. Please help me to write similar code for okhttp. I'm not familiar with okhttp and may be stuck here for a long time

class DiagnosisClient(
    userName: String,
    password: String,
    authServiceUrl: String,
    language: String,
    healthServiceUrl: String?
) {
    var token: AccessToken? = null
    private val language: String
    private val healthServiceUrl: String?
    private val httpclient: CloseableHttpClient
    @Throws(Exception::class)
    private fun LoadToken(username: String, password: String, url: String) {
        val keySpec = SecretKeySpec(
            password.toByteArray(),
            "HmacMD5"
        )
        var computedHashString = ""
        computedHashString = try {
            val mac: Mac = Mac.getInstance("HmacMD5")
            mac.init(keySpec)
            val result: ByteArray = mac.doFinal(url.toByteArray())
            val encoder = BASE64Encoder()
            encoder.encode(result)
        } catch (e: NoSuchAlgorithmException) {
            e.printStackTrace()
            throw Exception("Can not create token (NoSuchAlgorithmException)")
        } catch (e: InvalidKeyException) {
            e.printStackTrace()
            throw Exception("Can not create token (InvalidKeyException)")
        }
        val httpPost = HttpPost(url)
        httpPost.setHeader("Authorization", "Bearer $username:$computedHashString")
        token = try {
            val response: CloseableHttpResponse = httpclient.execute(httpPost)
            val objectMapper = ObjectMapper()
            if (response.getStatusLine().getStatusCode() !== HttpStatus.SC_OK) {
                RetrieveException(response, objectMapper)
            }
            val accessToken: AccessToken = objectMapper.readValue(
                response.getEntity().getContent(),
                AccessToken::class.java
            )
            accessToken
        } catch (e: ClientProtocolException) {
            e.printStackTrace()
            throw Exception("Can not create token (ClientProtocolException)")
        } catch (e: IOException) {
            e.printStackTrace()
            throw Exception("Can not create token (IOException)")
        }
    }

    @Throws(Exception::class)
    private fun RetrieveException(response: CloseableHttpResponse, objectMapper: ObjectMapper) {
        val errorMessage: String = objectMapper.readValue(
            response.getEntity().getContent(),
            String::class.java
        )
        System.out.println(
            "Resposne with status code: " + response.getStatusLine().getStatusCode()
                .toString() + ", error message: " + errorMessage
        )
        throw Exception(errorMessage)
    }

    init {
        httpclient = HttpClients.createDefault()
        this.healthServiceUrl = healthServiceUrl
        this.language = language
        LoadToken(userName, password, authServiceUrl)
    }
}
```


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source