【问题标题】:java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT in retrofit 2java.lang.IllegalStateException:预期 BEGIN_ARRAY 但在改造 2 中是 BEGIN_OBJECT
【发布时间】:2016-04-28 02:17:04
【问题描述】:

我尝试使用 GET 方法改造 2.x 中的参数获取所有数据 但响应是预期的 BEGIN_ARRAY 但在改造 2 中是 BEGIN_OBJECT

这是我访问 URL 服务的代码

public interface BukuResepMasakanAPI {
public static String baseURL = "http://10.108.233.76/buku_resep_masakan_service/";

//membuat Instance Retrofit
Retrofit client = new Retrofit.Builder()
        .baseUrl(baseURL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();


@POST("jenis_resep")
public Call<JenisResepModel> getJenisResep(@Body JenisResepModel model);

@GET("get_resep_by_jenis/{id_jenis_resep}")
public Call<List<DetailResepModel>> getDetailResep(@Path("id_jenis_resep") String id_jenis_resep);

}

这是我调用改造的代码

public void loadData(){
    BukuResepMasakanAPI apiService = BukuResepMasakanAPI.client.create(BukuResepMasakanAPI.class);
    DetailResepModel model = new DetailResepModel();
    Log.d("lappet",""+idJenisResep);
    Call<List<DetailResepModel>> call = apiService.getDetailResep(idJenisResep);

    //proses call
    call.enqueue(new Callback<List<DetailResepModel>>() {
        @Override
        public void onResponse(Call<List<DetailResepModel>> call, Response<List<DetailResepModel>> response) {
            List<DetailResepModel> resep = response.body();
            Log.d("idjenisresep",""+idJenisResep+" size "+resep.size());
        }

        @Override
        public void onFailure(Call<List<DetailResepModel>> call, Throwable t) {
            Toast.makeText(getApplicationContext(),"Failed to connect",Toast.LENGTH_SHORT).show();
            Log.d("failed", "" + t.toString());
        }
    });
}

希望你能帮我解决这个问题

【问题讨论】:

标签: android retrofit retrofit2


【解决方案1】:

您的错误是说它收到了一个 JSON 对象,但您的回调需要一个列表。

(我假设这是错误所指的方法)

@GET("get_resep_by_jenis/{id_jenis_resep}")
public Call<List<DetailResepModel>>

您应该尝试将其更改为

@GET("get_resep_by_jenis/{id_jenis_resep}")
public Call<DetailResepModel>

【讨论】:

  • 当我按照你的建议尝试时,它会返回对象大小 = 0,但我有数据要获取。
  • 这听起来比您收到的错误消息要好,但我无能为力
猜你喜欢
  • 2020-02-10
  • 2020-03-14
  • 1970-01-01
  • 2016-06-28
  • 2019-02-06
  • 1970-01-01
  • 1970-01-01
  • 2016-12-22
  • 1970-01-01
相关资源
最近更新 更多