0

I try to get all data using parameter in GET method retrofit 2.x but respon is Expected BEGIN_ARRAY but was BEGIN_OBJECT in retrofit 2

this is my code to access URL service

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

}

and this is my code to call retrofit

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

i hope you can help me to solve that problem

1

1 Answer 1

0

Your error is saying it received a single JSON object, but your Callback expected a list.

(I assume this is the method the error refers to)

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

You should try changing that to

@GET("get_resep_by_jenis/{id_jenis_resep}")
public Call<DetailResepModel>
Sign up to request clarification or add additional context in comments.

2 Comments

when I try like you recommended it will be return size of object = 0, but I have data to get.
That sounds better than the error message you were getting, but I can't help with that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.