如何正确地将数据解析到我的改装客户端中以执行 post 请求
How to parse data into my retrofit client correctly in order to preform a post request
我正在尝试通过 POST 请求将 JSON 发送到带有 Retrofit 的 rest API。我遇到的问题是,我似乎无法计算 out/use 改造所需的数据类型 (JSONArray, String, INT ...etc)
,以便它 POST 到我的休息 API。截至目前,我只尝试在字符串中硬编码 JSON 并将其解析为 Retrofit,希望它 POSTing 到其余的 API 并弄乱我的类型我用来改造 POST
的输入
当我尝试解析一个字符串时,我得到以下信息:com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
这个错误表明我没有解析 JSON。问题是我不知道要解析什么。
当我尝试解析 JSON 数组或 JSON 对象或任何其他类型的数据时,我的应用程序视图将无法加载并且我的应用程序会崩溃。
我的code
interface SimpleApi {
@POST("posttest.php")
suspend fun postit(@Body post: String): Phone
}
这是我的改进 API。字符串值是我认为给我带来麻烦的那个,Phone class 是数据 class 我正在用于我的 recylerview。
private fun TestGet() {
val gson = GsonBuilder()
.setLenient()
.create()
val api = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(SimpleApi::class.java)
GlobalScope.launch(Dispatchers.IO) {
val response = api.postit(test)//it wants a JSONarray
//the above wants some kind of JSON to work I was passing Strings instead
try {
//here lies the app logic and recylerview setup
}catch (e: Exception){
println("you messed up the connection some how")
}
}
}
这个函数就是我调用其余函数 API、解析和返回 JSON 以及处理业务逻辑的方式。
现在变量test
已经有了一些东西,原来它只是一个硬编码的字符串,就像这样private var test = "[\"text1\", \"text2\", \"text3\"]"
和private var test = """["text1", "text2", "text3"]"""
,这没有用,我也试过如上所述实施 GSON 转换器这不起作用。
感谢您的宝贵时间。
编辑
没有 addConverterFactory(GsonConverterFactory.create(gson))
我得到以下错误 FATAL EXCEPTION: DefaultDispatcher-worker-1
和 com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
行 .addConverterFactory(GsonConverterFactory.create(gson))
应用时意味着改造将您传递的每个有效负载视为 Json 对象。所以,当你传递一个简单的字符串时,你会得到那个错误。
一种可能的解决方法是将字符串数组转换为 json 数组,如此处 .
所述
还要检查服务器响应是否有效json,您可以通过添加日志拦截器来实现。
以下是添加拦截器的方法:
首先,在你的 build.gradle:
implementation "com.squareup.retrofit2:retrofit:2.6.0"
implementation "com.squareup.retrofit2:converter-gson:2.6.0"
implementation "com.squareup.okhttp3:logging-interceptor:3.11.0"
然后:
val gson = GsonBuilder()
.setLenient()
.create()
val builder = OkHttpClient().newBuilder()
builder.readTimeout(120, TimeUnit.SECONDS)
builder.connectTimeout(30, TimeUnit.SECONDS)
if (BuildConfig.DEBUG) {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BASIC
builder.addInterceptor(interceptor)
}
val client = builder.build()
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build()
我正在尝试通过 POST 请求将 JSON 发送到带有 Retrofit 的 rest API。我遇到的问题是,我似乎无法计算 out/use 改造所需的数据类型 (JSONArray, String, INT ...etc)
,以便它 POST 到我的休息 API。截至目前,我只尝试在字符串中硬编码 JSON 并将其解析为 Retrofit,希望它 POSTing 到其余的 API 并弄乱我的类型我用来改造 POST
当我尝试解析一个字符串时,我得到以下信息:com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
这个错误表明我没有解析 JSON。问题是我不知道要解析什么。
当我尝试解析 JSON 数组或 JSON 对象或任何其他类型的数据时,我的应用程序视图将无法加载并且我的应用程序会崩溃。
我的code
interface SimpleApi {
@POST("posttest.php")
suspend fun postit(@Body post: String): Phone
}
这是我的改进 API。字符串值是我认为给我带来麻烦的那个,Phone class 是数据 class 我正在用于我的 recylerview。
private fun TestGet() {
val gson = GsonBuilder()
.setLenient()
.create()
val api = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(SimpleApi::class.java)
GlobalScope.launch(Dispatchers.IO) {
val response = api.postit(test)//it wants a JSONarray
//the above wants some kind of JSON to work I was passing Strings instead
try {
//here lies the app logic and recylerview setup
}catch (e: Exception){
println("you messed up the connection some how")
}
}
}
这个函数就是我调用其余函数 API、解析和返回 JSON 以及处理业务逻辑的方式。
现在变量test
已经有了一些东西,原来它只是一个硬编码的字符串,就像这样private var test = "[\"text1\", \"text2\", \"text3\"]"
和private var test = """["text1", "text2", "text3"]"""
,这没有用,我也试过如上所述实施 GSON 转换器这不起作用。
感谢您的宝贵时间。
编辑
没有 addConverterFactory(GsonConverterFactory.create(gson))
我得到以下错误 FATAL EXCEPTION: DefaultDispatcher-worker-1
和 com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
行 .addConverterFactory(GsonConverterFactory.create(gson))
应用时意味着改造将您传递的每个有效负载视为 Json 对象。所以,当你传递一个简单的字符串时,你会得到那个错误。
一种可能的解决方法是将字符串数组转换为 json 数组,如此处 .
还要检查服务器响应是否有效json,您可以通过添加日志拦截器来实现。
以下是添加拦截器的方法:
首先,在你的 build.gradle:
implementation "com.squareup.retrofit2:retrofit:2.6.0"
implementation "com.squareup.retrofit2:converter-gson:2.6.0"
implementation "com.squareup.okhttp3:logging-interceptor:3.11.0"
然后:
val gson = GsonBuilder()
.setLenient()
.create()
val builder = OkHttpClient().newBuilder()
builder.readTimeout(120, TimeUnit.SECONDS)
builder.connectTimeout(30, TimeUnit.SECONDS)
if (BuildConfig.DEBUG) {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BASIC
builder.addInterceptor(interceptor)
}
val client = builder.build()
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build()