【发布时间】:2021-05-19 03:02:24
【问题描述】:
我正在尝试使用改造库发出我的第一个 POST 请求以使用户登录,但它不起作用,我不明白为什么。如果我发出 GET 请求,它可以工作,但是 POST 出现问题,我不明白为什么。 我的 API 在本地网络服务器上运行 我的 LoginService 代码:
private const val BASE_URL = "http://localhost:10000/api/"
/**
* Build the Moshi object that Retrofit will be using, making sure to add the Kotlin adapter for
* full Kotlin compatibility.
*/
private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
/**
* Use the Retrofit builder to build a retrofit object using a Moshi converter with our Moshi
* object.
*/
private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl(BASE_URL)
.build()
interface LoginApiService {
@Headers("Content-Type: application/json")
@POST("login")
suspend fun makeLogin(@Body usr: User): LoginResponse
}
/**
* A public Api object that exposes the lazy-initialized Retrofit service
*/
object LoginApi {
val retrofitService : LoginApiService by lazy { retrofit.create(LoginApiService::class.java) }
}
LoginResponse 类的代码
data class LoginResponse(
val token: String,
val expiration: Date,
val role: Int)
User 类的代码:
data class User(
val mail: String,
val pw: String
) : Parcelable
发出请求的 ViewModel 的代码:
private fun makeLogin(email: String, password: String) {
viewModelScope.launch {
try {
val usr = User(email, password)
val rsp = LoginApi.retrofitService.makeLogin(usr)
_isLogged.value = true
} catch (ex: Exception) {
_status.value = LoginStatus.ERROR
}
}
}
有人可以帮我解决这个问题吗?似乎它没有发送请求。 我的改造调用在 try-catch 块中的 logcat 中生成此错误
java.lang.IllegalArgumentException: Unable to create converter for class com.example.ticketapp.network.LoginResponse
for method LoginApiService.makeLogin
【问题讨论】:
-
您的 AndroidManifest.xml 文件中是否有 Internet 权限声明?
-
@VictorCold 是的,还有所有的毕业生
-
可以尝试在User类中添加@Parcelize注解。我不确定这是否能解决问题,但 AFAIK 仅从 Parcelable 继承是不够的。
-
但我已经有@parcelize
-
可能是因为您的“到期”字段是日期类型。基本上,JSON 只支持原始字段。我想在您的情况下,您可以尝试将日期更改为字符串或长。取决于您如何从服务器返回它。
标签: android kotlin retrofit retrofit2