Home > Back-end >  FATAL EXCEPTION: OkHttp Dispatcher, Expected BEGIN_ARRAY but was STRING at line 1 column 1 path
FATAL EXCEPTION: OkHttp Dispatcher, Expected BEGIN_ARRAY but was STRING at line 1 column 1 path

Time:01-27

I am trying to fetch first 10 coins from coinmarket API. Highlighted the part in the following code which is giving me an error. I don't understand why it's leading to the crashing of my app. Please help!

private fun loadFirst10Coin(){
        client = OkHttpClient()
        request = Request.Builder()
            .url(String.format("https://api.coinmarketcap.com/v1/ticker/?start=0&limit=10"))
            .build()

        client.newCall(request)
            .enqueue(object: Callback
            {
                override fun onFailure(call: Call, e: IOException) {
                    Log.d("Error", e.toString())
                }

                override fun onResponse(call: Call, response: Response) {
                    val body = response?.body!!.string()
                    val gson = Gson()
//This line is giving me error// 
**items = gson.fromJson(body, object: TypeToken<List<CoinModel>>() {}.type)**
                    runOnUiThread{
                        adapter.updateData(items)


                    }
                }


            })
    }

This is how my logcat looks

2022-01-27 16:14:01.224 22322-14470/com.example.cryptocheck E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
    Process: com.example.cryptocheck, PID: 22322
    com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
        at com.google.gson.Gson.fromJson(Gson.java:975)
        at com.google.gson.Gson.fromJson(Gson.java:928)
        at com.google.gson.Gson.fromJson(Gson.java:877)
        at com.example.cryptocheck.MainActivity$loadFirst10Coin$1.onResponse(MainActivity.kt:81)
        at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:923)
     Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
        at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:349)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
        at com.google.gson.Gson.fromJson(Gson.java:963)

CodePudding user response:

The error you are getting is because string is not starting from "[".

Friendly Reminder: You should NOT be using GSON anymore. Jake Wharton, one of the projects maintainers, suggests using Moshi, Jackson or kotlinx.serialization.

CodePudding user response:

All the errors of this format "Expected _______ but was ______ at line __ coloumn __" relates to deserialization errors.

What that means is the data that you got via the network call is of a particular format and you're parsing it as something else.

in your case "Expected BEGIN_ARRAY but was STRING" means your JSON response starts with '[' but you're parsing it as a string.

Using Retrofit in conjunction with a suitable converter factory (GSON,Moshi,Jackson etc) and having your json response in the form of a data class with a plugin like JSON to Kotlin Class will reduce these errors a lot.

Also the endpoint might return something else instead of the intended JSON like HTML for example so using a networking library would make handling it easier

  •  Tags:  
  • Related