0

I get Village,City, Dist using ZipPostal code Api.Retrofit Network library and kotlin coroutines. I used https://api.postalpincode.in/pincode/{PINCODE}. Give Error is "com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $"

[{"Message":"Number of pincode(s) found:3","Status":"Success","PostOffice":
[{"Name":"Baroda House","Description":null,"BranchType":"Sub Post Office","DeliveryStatus":"Non-Delivery","Circle":"Delhi","District":"Central Delhi","Division":"New Delhi Central","Region":"Delhi","Block":"New Delhi","State":"Delhi","Country":"India","Pincode":"110001"},
{"Name":"Bengali Market","Description":null,"BranchType":"Sub Post Office","DeliveryStatus":"Non-Delivery","Circle":"Delhi","District":"Central Delhi","Division":"New Delhi Central","Region":"Delhi","Block":"New Delhi","State":"Delhi","Country":"India","Pincode":"110001"},
{"Name":"Supreme Court","Description":null,"BranchType":"Sub Post Office","DeliveryStatus":"Non-Delivery","Circle":"Delhi","District":"Central Delhi","Division":"New Delhi Central","Region":"Delhi","Block":"New Delhi","State":"Delhi","Country":"India","Pincode":"110001"}]}]

Above, Json Data have first fall Json Array after that Json Object so, its give above error.

Pincode.kt

data class Pincode(
    @SerializedName("Message")
    val Message : String,
    @SerializedName("Status")
    val Status : String,
    @SerializedName("PostOffice")
    val PostOffice : List<PostOffice>
)

PostOffice.kt

data class PostOffice(
    @SerializedName("Name")
    val Name : String,
    @SerializedName("BranchType")
    val BranchType : String,
    @SerializedName("DeliveryStatus")
    val DeliveryStatus : String,
    @SerializedName("Circle")
    val Circle : String,
    @SerializedName("District")
    val District : String,
    @SerializedName("Division")
    val Division : String,
    @SerializedName("Region")
    val Region : String,
    @SerializedName("Block")
    val Block : String,
    @SerializedName("State")
    val State : String,
    @SerializedName("Country")
    val Country : String,
    @SerializedName("Pincode")
    val Pincode : String,

)

ApiClient .kt

object ApiClient {

  val apiInterfce: ApiInterface = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .client(getClient())
        .addConverterFactory(GsonConverterFactory.create())
        .build()
        .create(ApiInterface::class.java)

}

fun getClient() : OkHttpClient {

    val client = OkHttpClient.Builder()
        .connectTimeout(10, TimeUnit.MINUTES)
        .readTimeout(10, TimeUnit.MINUTES)
        .build()

    return client
}

ApiInterface.kt

interface ApiInterface {


    @GET("pincode/{PINCODE}")
    suspend fun pinCode(@Path("PINCODE") pinCode: String?): Response<Pincode>
}

MainActivity.kt

  private lateinit var binding : ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityPinCodeVerificationBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val etPinCode = binding.etPinCode.text.toString()

        GlobalScope.launch (Dispatchers.IO){
            val api  = ApiClient.apiInterfce.pinCode(etPinCode)
             if (api.isSuccessful && api.body() != null){
                val data = api.body()!!
              
                data.Status.let {
                    binding.etAddress.setText(it)
                }
                data.PostOffice.let {
                    binding.etVillage.setText(it?.component1()?.Name)
                    binding.etTaluka.setText(it?.component2()?.Block)
                    binding.etDist.setText(it?.component3()?.District)
                    binding.etState.setText(it?.component4()?.State)
                    binding.etCountry.setText(it?.component5()?.Country)
                }
            }

        }
    }

when application run after crash its give this error com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $. how to its error fix and pincode verified this Api and after show data village, city, Dist and state. Any other postlpincode api available suggeste me.

Pinkesh
  • 61
  • 3
  • 12

1 Answers1

0

This is because of in your response you have Array and while you receive that response you access that response using object.

You have to do line below when you receive response.

interface ApiInterface {


    @GET("pincode/{PINCODE}")
    suspend fun pinCode(@Path("PINCODE") pinCode: String?): Response<List<Pincode>>
}
Mehul Kabaria
  • 6,404
  • 4
  • 25
  • 50