Search code examples
javaandroidgsonretrofit2laravel-api

Fatal Exception: java.lang.RuntimeException - Caused by java.lang.IllegalStateException. Expected BEGIN_ARRAY but was BEGIN_OBJECT


I am getting reports of crashes in the Android Pre-Launch report. Everything works fine on my emulator and three physical test devices that I have. The error being reported is the following

Fatal Exception: java.lang.RuntimeException

An error occurred while executing doInBackground()

android.os.AsyncTask$3.done (AsyncTask.java:353)
java.util.concurrent.FutureTask.finishCompletion (FutureTask.java:383)
java.util.concurrent.FutureTask.setException (FutureTask.java:252)
java.util.concurrent.FutureTask.run (FutureTask.java:271)
android.os.AsyncTask$SerialExecutor$1.run (AsyncTask.java:245)
java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1162)
java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:636)
java.lang.Thread.run (Thread.java:764)

Caused by java.lang.IllegalStateException

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read (CollectionTypeAdapterFactory.java)
retrofit2.converter.gson.GsonResponseBodyConverter.convert (GsonResponseBodyConverter.java:1)
retrofit2.OkHttpCall.a (OkHttpCall.java:59)
retrofit2.OkHttpCall.execute (OkHttpCall.java:73)
retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall.execute (DefaultCallAdapterFactory.java:2)
com.cn.managers.CNServiceManager.fetchChapters (CNServiceManager.java:2)
com.cn.tasks.FetchChaptersTask.doInBackground (FetchChaptersTask.java:2)
com.cn.tasks.FetchChaptersTask.doInBackground (FetchChaptersTask.java:2)

The doInBackground method in FetchChaptersTask class is as follows

@Override
protected ArrayList<Chapter> doInBackground(Void... voids) {
    return CNServiceManager.fetchChapters(subjectId, purpose);
}

In CNServiceManager the method fetchChapters (subjectId, purpose) is defined. This method is basically getting a list of chapters from the API using Retrofit2 and GSON Converter. The API call is given as follows. The CNServiceManager class is given below:

public class CNServiceManager {

    public static final String PREF_AUTHENTICATION_TOKEN    = "CNServiceManager_authentication_token";
    public static final String PREF_SESSION_ID              = "CNServiceManager_session_id";

    private interface CNService {

        @GET("v2/chapters/{subjectId}/{purpose}")
        Call<ArrayList<Chapter>> fetchChapters(@Header("Authentication-Token") String authentication,
                                               @Path("subjectId") long subjectId,
                                               @Path("purpose") String purpose);
    }

    /**
     * Authorization token
     */
    private static String mAuthorizationToken;

    /**
     * The main interface to server
     */
    private static CNService mCNService;

    /**
     * Initialize
     *
     * @param context
     */
    public static void init(Context context) {

        OkHttpClient httpClient = new OkHttpClient.Builder()
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.0.105:8000/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient)
                .build();

        long applicationId = 9;
        String applicationSecret = "AppSecret";
        mAuthorizationToken = String.format(Locale.ENGLISH, "%d/%s", applicationId, applicationSecret);

        mCNService = retrofit.create(CNService.class);
    }

    /**
     * Returns authorization code
     *
     * @return
     */
    private static String getAuthorizationToken() {
        return mAuthorizationToken;
    }

    /**
     * Returns authentication code
     *
     * @return
     */
    private static String getAuthenticationToken() {
        return PreferencesManager.getString(PREF_AUTHENTICATION_TOKEN, null);
    }

    /**
     * Returns chapters
     *
     * @param subjectId
     * @param purpose
     * @return
     */
    public static ArrayList<Chapter> fetchChapters(long subjectId, String purpose) {
        try {
            Call<ArrayList<Chapter>> call = mCNService.fetchChapters(getAuthenticationToken(), subjectId, purpose);
            Response<ArrayList<Chapter>> response = call.execute();
            return response.body();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

Finally, Here's how the Chapter model is coded.

public class Chapter {

    /**
     * Id of the chapter
     */
    private long id;

    /**
     * Name of the chapter
     */
    private String name;

    /**
     * Chapter
     *
     * @param id
     * @param name
     */
    public Chapter(long id, String name) {
        this.id = id;
        this.name = name;
    }

    /**
     * Chapter
     */
    public Chapter() {
        this(0, null);
    }

    /**
     * Returns the id of the chapter
     *
     * @return
     */
    public long getId() {
        return id;
    }

    /**
     * Sets the id of the chapter
     *
     * @param id
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * Returns the name of the chapter
     *
     * @return
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the name of the chapter
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }
}

As I mentioned in the start, I am getting the crash reports in Google Play Pre-Launch Reports and in some live users. But on my devices and emulator, it is not crashing.


Solution

  • Please don't use AsyncTask for Retrofit. Instead, you can you use Reterofit callback as following.

    public static void init(Context context) {
    
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .build();
    
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.0.105:8000/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient)
                .build();
    
        long applicationId = 9;
        String applicationSecret = "AppSecret";
        mAuthorizationToken = String.format(Locale.ENGLISH, "%d/%s", applicationId, applicationSecret);
    
        mCNService = retrofit.create(CNService.class);
    
        // additional code instead of AsyncTask
        Call<List<Chapter>> call = mCNService.fetchChapters();
        call.enqueue(new Callback<List<Chapter>>() {
            @Override
            public void onResponse(Call<List<Chapter>> call, Response<List<Chapter>> response) {
                progressDoalog.dismiss();
                generateDataList(response.body());
            }
    
            @Override
            public void onFailure(Call<List<Chapter>> call, Throwable t) {
                progressDoalog.dismiss();
                Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
            }
        });
    }
    

    I hope this will solve your problem