0

I have a problem whit a gson deserialize

it's my json

[{"compteurdi":"00","numero":"DI00","message":"LOLOL","equipement":"LE TRUC","etat":"DEMANDER","demandeur":"MOI","datedi":"020202"}]

my kotlin 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
    }

and my deserialise function

    class Deserializer:ResponseDeserializable<Array<DI>>{
    override fun deserialize(content: String): Array<DI>? = Gson().fromJson(content,Array<DI>::class.java)
}

And i have this error

I/System.out: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $

I do not understand what is wrong I really feel that everything is correct and I have done multiple different tests.

Can you help me.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Are you testing this code on an emulator or on a real device? It may not work on a device https://stackoverflow.com/questions/35441481/connection-to-localhost-10-0-2-2-from-android-emulator-timed-out – atarasenko Oct 05 '18 at 08:47

1 Answers1

0

Your json response is array and you define String based to get that way show error.

And your json has error paste your json data in this link show error.. http://www.jsonschema2pojo.org/

Try to used Retrofit it is to work on api integration.

add below dependency into app level gradle file.

// 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'

after make pojo class for your api response..

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 + '\'' + 
        "}";
    }
  }

Retrofit object creation..

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
    }
}

}

after make interface for api calling..

interface ApiInterface {
@GET(NetworkConstant.DATA)
fun getData(): Call<List<Hero>> // define your response class

}

call api into fragment or activity..

 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)
            }
        }

    })
}