使用改造来调用同一类中的两个API

时间:2018-09-26 11:34:02

标签: android retrofit2

调用两个不同的API时遇到问题。使用改造调用第一个API时没有问题,但是当它到达第二个改造调用时,会发生错误。以下是我的代码。

//for first retrofit call:

apiInterface = ApiClient.getApiClient().create(LekhaDrillInterface.class);
    Call<LekhaDrillModel> call = apiInterface.getData(id, memberId);
    call.enqueue(new Callback<LekhaDrillModel>() {

        @Override
        public void onResponse(Call<LekhaDrillModel> call, Response<LekhaDrillModel> response) {
            posts = Collections.singletonList(response.body());
            adapter = new LekhaBolpatraDrillAdapter(posts, getApplicationContext());
            rvLekhaDrill.setAdapter(adapter);
            progressBar.setVisibility(View.GONE);
        }

        @Override
        public void onFailure(Call<LekhaDrillModel> call, Throwable t) {
            Log.e("error", t.getMessage());
        }
    });

这里没有错误,并且在适配器中设置了数据

//2nd retrofit call

private void callRetrofitListData(){
        pariyojanaInterfaces = ApiClient.getApiClient().create(PariyojanaInterface.class);
        Log.e("memberIDIDID", String.valueOf(memberId));
        Call  <Data> call1 = pariyojanaInterfaces.getData(memberId);
        call1.enqueue(new Callback<Data>() {
            @Override
            public void onResponse(Call<Data> call, Response<Data> response) {
                datas = (List<Data>) response.body();

                if (response.body()!=null){
                    Log.d("jchhec","chekc");
                }
                itemListAdapter = new ItemListAdapter(getApplicationContext(), datas);
                rv.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                rv.setAdapter(itemListAdapter);
            }

            @Override
            public void onFailure(Call<Data> call1, Throwable t) {
                Log.e("error", t.getMessage());
            }
        });
}

错误在行

Call  <Data> call1 = pariyojanaInterfaces.getData(memberId);

,错误是

Unable to create call adapter for interface retrofit2.Call for method PariyojanaInterface.getData

PariyojanaInterface.java

 public interface PariyojanaInterface { @POST("projectBudget/pariyojanaListForMobile") Call getData(@Query("memberId") int memberId); 

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

首先,您必须将PariyojanaInterface.java更改为

public interface PariyojanaInterface {
       @POST("projectBudget/pariyojanaListForMobile") Call<List<Data>> getData(@Query("memberId") int memberId);
}

我不知道您的数据是什么,因此您必须将List<Data>替换为List<yourData>

您必须将new Callback<Data>更改为new Callback<List<Data>