错误java.net.sockettimeoutexception:Kotlin中的超时
我得到了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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论