注册时触发Use JsonReader.setLenient(true)错误:数据库注册成功但Retrofit请求抛出异常无法获取结果
Hey Rafael, let's break down why this error is happening and fix it step by step.
What's Causing the Issue?
The core problem here is that even though you've set up Moshi with .asLenient(), your Retrofit service method is returning a String—Retrofit doesn't use Moshi to parse String responses. Instead, it uses its built-in string converter, which doesn't respect the lenient JSON parsing rules you configured.
Since your endpoint is marked with Content-Type: application/json, the default string converter still tries to validate the response as strict JSON. When the server returns malformed JSON (or even plain text like "success"), it throws that Use JsonReader.setLenient(true) error—even though the database write succeeded.
Fixes You Can Implement
Let's go through the most common solutions based on what your server actually returns:
1. Use a Data Class for the Response (Recommended)
If your server is supposed to return JSON (even if it's malformed), switch your service method to return a data class instead of String. This will force Retrofit to use your lenient Moshi converter for parsing.
First, create a data class matching your server's response (adjust fields to match what's actually returned):
data class ResponseCadastro( @Json(name = "status") val status: String, @Json(name = "message") val message: String )
Then update your Services.kt:
@Headers("Content-Type: application/json") @POST("user") suspend fun createUser(@Body params: RequestCadastro): ResponseCadastro
Finally, adjust your repository and view model to handle the new response type. This way, Moshi's lenient mode will kick in and tolerate malformed JSON.
2. Handle Plain Text Responses
If your server returns plain text (like "Registration successful") instead of JSON, you have two options:
Option A: Update Request Headers
Tell Retrofit to accept plain text by adding an Accept header:
@Headers( "Content-Type: application/json", "Accept: text/plain" ) @POST("user") suspend fun createUser(@Body params: RequestCadastro): String
Option B: Add a Scalars Converter
Add Retrofit's ScalarsConverterFactory to handle string responses properly. Make sure to add it before Moshi's converter so Retrofit uses it first for String types:
val retrofit: Retrofit = Retrofit.Builder() .client(client) .baseUrl(url) .addConverterFactory(ScalarsConverterFactory.create()) // Add this first .addConverterFactory(MoshiConverterFactory.create().asLenient()) .build()
3. Get Raw ResponseBody (Full Control)
If you want to bypass Retrofit's automatic parsing entirely, have your service method return ResponseBody instead of String, then manually convert it to a string:
Update Services.kt:
@Headers("Content-Type: application/json") @POST("user") suspend fun createUser(@Body params: RequestCadastro): ResponseBody
Then adjust your repository:
suspend fun createUser(...): String { val request = RequestCadastro(...) val responseBody = cadastro.createUser(request) return responseBody.string() // Reads raw response without JSON validation }
Quick Debug Tip
First, use Postman or curl to call your /user endpoint and check exactly what the server returns. This will tell you if it's malformed JSON, plain text, or something else—helping you pick the right fix faster.
内容的提问来源于stack exchange,提问作者Rafael Souza

