Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

380
Views
java.lang.IllegalStateException:JSON OBJECT y JSON ARRAY al analizar Json usando GSON

Visualización de datos JSON del servidor en actividad usando Retrofit. Conversión de datos JSON a través de gson.

Da error: "java.lang.IllegalStateException: se esperaba BEGIN_ARRAY pero era BEGIN_OBJECT en la línea 1 columna 2 ruta $"

Formato JSON:

 {"success":1,"company":[{"Cmp_Id":"1","Cmp_Name":"ABC","GSTIN":"AAAA"}]}

Código:

 class Company { //@SerializedName("Cmp_Id") var Cmp_Id : Int = 0 //@SerializedName("success") //val success : String = "" //@SerializedName("Cmp_Name") var Cmp_Name : String? = "" //@SerializedName("GSTIN") var GSTIN : String? = "" } class CompanyList { val success : String = "" lateinit var company : ArrayList<Company> } interface APIInterface { @GET("Company.php") fun getCompanyData() : Observable<List<CompanyList>> } object APIClient { val BASE_URL = "http://10.0.2.2/" var retrofit:Retrofit? = null val apIClient:Retrofit? get() { if (retrofit == null) { retrofit = Retrofit.Builder(). baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build() } return retrofit } }

MainActivity.kt

 private fun fetchData(){ /* compositeDisposable.add(api.getCompanyData() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { companyList-> displayData(companyList) } )*/ val retrofit = APIClient.apIClient if (retrofit != null) { api = retrofit.create(APIInterface::class.java) } api.getCompanyData() .subscribeOn(Schedulers.io()) .unsubscribeOn(Schedulers.computation()) .observeOn(AndroidSchedulers.mainThread()) .subscribe({ companyAdapter.setCompany(it.component1().company) },{ Toast.makeText(applicationContext, it.message, Toast.LENGTH_SHORT).show() }) }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

La interfaz de actualización:

 interface APIInterface { @GET("Company.php") fun getCompanyData() : Observable<List<CompanyList>> }

Sugerencias de actualización que espera una lista de CompanyList . Sin embargo, el json que recibe es una CompanyList única. Esto es lo que le dice el error gson que está recibiendo: se esperaba que comenzara con una matriz, pero era un objeto.

Cambiar la interfaz de actualización a:

 interface APIInterface { @GET("Company.php") fun getCompanyData() : Observable<CompanyList> }

insinuará la actualización de que espera un solo objeto de tipo CompanyList y esto debería funcionar para el json que ha publicado.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!