'Error java.net.SocketTimeoutException: timeout in Kotlin

I'm getting that java.net.SocketTimeoutException: timeout but when I'm trying the post request with Postman I'm getting the response in 500 milliseconds, I tried some with client.run and with client.newCall but still not getting a response, does anyone know what could be the problem?

package com.example.onlineshopping_mobileapp_23643_lucianogimenez

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import okhttp3.*
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.IOException


class MainActivity : AppCompatActivity() {

    var usersList = ArrayList<User>()
    private lateinit var usersListGson: ArrayList<User>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        supportActionBar?.title = "Login Page"

        val bundle: Bundle? = intent.extras
        val jsonNewUser = bundle?.getString(RegisterActivity.NEW_USER_KEY)
        //Log.i("lucho", "Pre if main $jsonNewUser")

        if (jsonNewUser != null) {
            val gson = GsonBuilder().create()
            val newUser = gson.fromJson(jsonNewUser, User::class.java)
            //Log.i("lucho", "object $newUser")
            usersList.add(newUser)
        }

        val loginButtonClick = findViewById<Button>(R.id.login_login_button)
        loginButtonClick.setOnClickListener {
            if (findViewById<EditText>(R.id.user_name_login).text.toString() != "" &&
                findViewById<EditText>(R.id.password_login).text.toString() != ""
            ) {
                val username = findViewById<EditText>(R.id.user_name_login).text.toString()
                val password = findViewById<EditText>(R.id.password_login).text.toString()
                fetchJsonData(username, password)
            } else {
                Toast.makeText(
                    this,
                    "You have to write a username and/or password",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }

        val registerButtonClick = findViewById<Button>(R.id.register_button_login)
        registerButtonClick.setOnClickListener {
            val intent = Intent(this, RegisterActivity::class.java)
            startActivity(intent)
        }

    }

    private fun fetchJsonData(username: String, password: String) {
        val url = "https://fakestoreapi.com/auth/login"
        val client = OkHttpClient()
        val userLogin = UserLogin (username, password)
        val gson = GsonBuilder().create()
        val jsonLoginUser = gson.toJson(userLogin)
        println(jsonLoginUser)
        //I/System.out: {"password":"jklg*_56","username":"derek"}
        val request  = Request.Builder()
                              .url(URL)
                              .post(jsonLoginUser.toRequestBody())
                              .build()
        client.run{
            newCall(request).enqueue(object : Callback{
                override fun onFailure(call: Call, e: IOException) {
                    println("error: $e")
                }
                override fun onResponse(call: Call, response: Response) {
                    println("success: ${response.body}")
                }

            })
        }
    }
}


Sources

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

Source: Stack Overflow

Solution Source