我在Android Pre-Launch报告中收到崩溃报告。在我的仿真器和我拥有的三个物理测试设备上,一切正常。报告的错误如下

致命异常:java.lang.RuntimeException

执行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)


由java.lang.IllegalStateException引起

预期为BEGIN_ARRAY,但位于第1行第2列路径$的BEGIN_OBJECT

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)


FetchChaptersTask类中的doInBackground方法如下

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


在CNServiceManager中,定义了方法fetchChapters(subjectId,用途)。该方法基本上是使用Retrofit2和GSON Converter从API获取各章的列表。 API调用如下。 CNServiceManager类如下所示:

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

}


最后,这是Chapter模型的编码方式。

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


正如我在开始时提到的,我在Google Play推出前报告和一些实时用户中获得了崩溃报告。但是在我的设备和仿真器上,它没有崩溃。

最佳答案

请不要使用AsyncTask进行改造。相反,您可以按以下方式使用Reterofit回调。

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


我希望这能解决您的问题

10-06 13:21