我对gson反序列化有问题
这是我的json
[{"compteurdi":"00","numero":"DI00","message":"LOLOL","equipement":"LE TRUC","etat":"DEMANDER","demandeur":"MOI","datedi":"020202"}]
我的科特琳httpget
"http://10.0.2.2:8080/WebApi/V1/index.php?op=getdis".httpGet().responseObject(DI.Deserializer())
{ request, response, result ->
val(disw,err) = result
println(err)
disw?.forEach { DI ->
dis.add(DI)
}
adapter = DiAdapter(dis,this )
val recyclerView = findViewById(R.id.di_recycler_view) as RecyclerView
recyclerView.layoutManager = LinearLayoutManager(this) as RecyclerView.LayoutManager?
recyclerView.adapter = adapter
}
和我的反序列化功能
class Deserializer:ResponseDeserializable<Array<DI>>{
override fun deserialize(content: String): Array<DI>? = Gson().fromJson(content,Array<DI>::class.java)
}
我有这个错误
I/System.out: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
我不明白出什么问题了,我真的觉得一切都正确,并且我已经进行了多种不同的测试。
你能帮我吗?
答案 0 :(得分:0)
您的json响应是数组,您定义基于String的方式来显示错误。
并且您的json错误将您的json数据粘贴到此链接显示错误中。 http://www.jsonschema2pojo.org/
尝试使用Retrofit来进行api集成。
将以下依赖项添加到应用程序级别的gradle文件中。
// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
在为您的api响应创建pojo类之后。
public class ResponseUserData{
@SerializedName("datedi")
private String datedi;
@SerializedName("numero")
private String numero;
@SerializedName("equipement")
private String equipement;
@SerializedName("compteurdi")
private String compteurdi;
@SerializedName("demandeur")
private String demandeur;
@SerializedName("message")
private String message;
@SerializedName("etat")
private String etat;
public void setDatedi(String datedi){
this.datedi = datedi;
}
public String getDatedi(){
return datedi;
}
public void setNumero(String numero){
this.numero = numero;
}
public String getNumero(){
return numero;
}
public void setEquipement(String equipement){
this.equipement = equipement;
}
public String getEquipement(){
return equipement;
}
public void setCompteurdi(String compteurdi){
this.compteurdi = compteurdi;
}
public String getCompteurdi(){
return compteurdi;
}
public void setDemandeur(String demandeur){
this.demandeur = demandeur;
}
public String getDemandeur(){
return demandeur;
}
public void setMessage(String message){
this.message = message;
}
public String getMessage(){
return message;
}
public void setEtat(String etat){
this.etat = etat;
}
public String getEtat(){
return etat;
}
@Override
public String toString(){
return
"ResponseUserData{" +
"datedi = '" + datedi + '\'' +
",numero = '" + numero + '\'' +
",equipement = '" + equipement + '\'' +
",compteurdi = '" + compteurdi + '\'' +
",demandeur = '" + demandeur + '\'' +
",message = '" + message + '\'' +
",etat = '" + etat + '\'' +
"}";
}
}
创建改造对象。
class ApiClient {
companion object {
val BASE_URL = "https://simplifiedcoding.net/demos/"
var retrofit: Retrofit? = null
fun getClient(): Retrofit? {
if (retrofit == null) {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient.Builder().apply {
readTimeout(20, TimeUnit.SECONDS)
writeTimeout(20, TimeUnit.SECONDS)
connectTimeout(20, TimeUnit.SECONDS)
addInterceptor(interceptor)
addInterceptor { chain ->
var request = chain.request()
request = request.newBuilder()
.build()
val response = chain.proceed(request)
response
}
}
retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client.build())
.addConverterFactory(GsonConverterFactory.create())
.build()
}
return retrofit
}
}
}
在用于api调用的make接口之后。
interface ApiInterface {
@GET(NetworkConstant.DATA)
fun getData(): Call<List<Hero>> // define your response class
}
将api称为片段或活动。
private fun getHeroData() {
val dialog= ProgressDialog(mContext)
showProgress(dialog)
var apiInterface: ApiInterface = ApiClient.getClient()!!.create(ApiInterface::class.java)
var hero: Call<List<Hero>>
hero = apiInterface.getData()
hero.enqueue(object : Callback<List<Hero>> {
override fun onFailure(call: Call<List<Hero>>?, t: Throwable?) {
closeDialog(dialog)
Toast.makeText(mContext, t?.message, Toast.LENGTH_SHORT).show()
Log.d("Error:::",t?.message)
}
override fun onResponse(call: Call<List<Hero>>?, response: Response<List<Hero>>?) {
mHeroDataList.clear()
if (response != null && response.isSuccessful && response.body() != null) {
closeDialog(dialog)
mHeroDataList .addAll(response.body()!!)
setAdapter(mHeroDataList)
}
}
})
}