错误java.net.sockettimeoutexception:Kotlin中的超时

发布于 2025-01-26 11:09:38 字数 3431 浏览 3 评论 0原文

我得到了java.net.sockettimeoutexception:超时,但是当我与Postman尝试发布请求时,我会在500毫秒中获得响应,我尝试了一些client.run.run和<<代码> client.newcall ,但仍然没有得到响应,有人知道可能是什么问题吗?

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}")
                }

            })
        }
    }
}

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}")
                }

            })
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文