【问题标题】:ArrayIndexOutOfBoundsException - Retrofit 2.4.0 CallArrayIndexOutOfBoundsException - Retrofit 2.4.0 调用
【发布时间】:2018-09-11 09:46:41
【问题描述】:

我正在尝试使用retrofit 拨打POST 电话。它在某些设备上运行良好,但在操作系统版本为6.0.1Nexus 5 上崩溃。

但现在我面临一个例外

art/runtime/thread.cc:1344] 抛出新异常 'length=1903; index=3147' 出现意外未决异常: java.lang.ArrayIndexOutOfBoundsException:长度=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'

这是我得到ArrayIndexOutOfBoundsException的API调用

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

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

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

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

TIA

【问题讨论】:

  • 也贴出相关代码
  • @VivekMishra 我已经添加了我得到异常 APIClient.getAPIService().getEffects(Constants.apiKey) 的代码。你需要什么让我知道?所以,我可以更新问题。
  • 您能分享一下 JSON 响应吗?
  • @AtifAbbAsi 应用在 API 命中前崩溃。
  • @Sunny 其他 API 工作正常吗?

标签: java android retrofit2 indexoutofboundsexception


【解决方案1】:

我也遇到了同样的问题。在我的情况下,我使用 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) {

                }
            });

我希望它对你有用。

【讨论】:

    【解决方案2】:

    我使用下一个版本的库:

    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    

    当我遇到错误时,我正在创建 Retrofit 实例,如下所示:

    Retrofit.Builder()
            .baseUrl(API_ENDPOINT)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    

    更改GsonConverterFactory 初始化解决了问题:

    Retrofit.Builder()
            .baseUrl(API_ENDPOINT)
            .addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
            .build()
    

    【讨论】:

      猜你喜欢
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      • 2018-02-13
      • 2019-09-09
      • 2017-04-22
      • 2020-04-05
      • 1970-01-01
      • 2015-08-15
      相关资源
      最近更新 更多