首页 > 解决方案 > ArrayIndexOutOfBoundsException - Retrofit 2.4.0 调用

问题描述

我正在尝试POST使用retrofit. 它在某些设备上运行良好,但在带有 OS 版本的Nexus 5上崩溃6.0.1

但现在我面临一个例外

art/runtime/thread.cc:1344] 抛出新异常 'length=1903; index=3147' 出现意外挂起异常:java.lang.ArrayIndexOutOfBoundsException: length=1903; 索引=3147

我知道有很多问题但是对我没有任何帮助。我根据给定的帖子尝试了所有内容,例如thisthisthis等。有些人通过禁用Instant Run解决了这个问题。但这对我不起作用。

我正在使用这些依赖项进行改造:

implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'

这是我的项目级 gradle 文件:

classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.google.gms:google-services:4.0.1'

这是我得到的 API 调用ArrayIndexOutOfBoundsException

APIClient.getAPIService().getEffects(Constants.apiKey).enqueue(//callback);

这是我声明调用的 API 接口:

@FormUrlEncoded
@POST("/getEffects")
Call<GetEffectResp> getEffects(@Field("apikey") String apikey);

如果有人解决了这个问题,请帮我弄清楚。

TIA

标签: javaandroidretrofit2indexoutofboundsexception

解决方案


我也面临同样的问题。在我的情况下,我使用JsonElement而不是Pojo 类来解决它。

下面是回调。你可以试试...

APIClient.getAPIService().getEffectsInternal(Constants.apiKey).enqueue(new Callback<JsonElement>() {
                @Override
                public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {

                   Log.e("",""+response.body());


                }

                @Override
                public void onFailure(Call<JsonElement> call, Throwable t) {

                }
            });

我希望这个对你有用。


推荐阅读