java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在改进2

时间:2016-04-28 02:17:04

标签: android retrofit retrofit2

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

这是我访问网址服务的代码

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

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

1 个答案:

答案 0 :(得分:0)

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

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

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

您应该尝试将其更改为

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