简体   繁体   English

Android Fragment 中的 Retrofit2 如何正确实现?

[英]Retrofit2 in Android Fragment how to implement correctly?

I'm using Retrofit 2 to consume an API.我正在使用 Retrofit 2 来使用 API。 I have a service (interface) which returns a list:我有一个返回列表的服务(接口):

@GET("atenciones")
Call<List<Atencion>> getAtenciones(@Query("medico_id") int id, @Query("date1") String date1, @Query("date2") String date2)

Where should I do the request?我应该在哪里做请求? In the MainActivity which contains the Fragment and send the result list using Bundle ?在包含FragmentMainActivity中并使用Bundle发送结果列表? or should do the request in the Fragment?或者应该在片段中做请求? this is a list not a single object.这是一个列表,而不是单个对象。 Which is the correct way?哪个是正确的方法?

Edit编辑

Try to call retrofit in Fragment, this is my code:尝试在 Fragment 中调用 retrofit,这是我的代码:

public class FragmentRendicion extends Fragment {

private LinearLayoutManager layoutManager;
View rootView;
APIService api;

public FragmentRendicion() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.fragment_rendicion, container, false);
    api= ApiUtils.getAPIService();
        getAtenciones();
    return rootView;
}

private void getAtenciones() {
//using static parameters
    Call<List<Atencion>> call= api.getAtenciones(293,"2014-10-13","2014-10-13");
    call.enqueue(new Callback<List<Atencion>>() {
        @Override
        public void onResponse(Call<List<Atencion>> call, Response<List<Atencion>> response) {
            System.out.println("estamos aquiiii "+response.message());
            List<Atencion> atenciones= response.body();
            layoutManager= new LinearLayoutManager(getActivity());
            RecyclerView recycler= (RecyclerView)rootView.findViewById(R.id.rvRendicion);
            recycler.setLayoutManager(layoutManager);
            RvRendicionAdapter rvAdapter= new RvRendicionAdapter(atenciones);
            recycler.setAdapter(rvAdapter);


        }

        @Override
        public void onFailure(Call<List<Atencion>> call, Throwable t) {
            System.out.println("FALLOOOOO:  "+t.getMessage());//HERE RETUNRS NULL
        }
    });

}
}

Can someone tell me is if it correct way to call retrofit2 in fragment?有人能告诉我在片段中调用 retrofit2 的方法是否正确?

It depends on the structure of your code but since you are using fragments my best guess is you should do it in the Fragment .这取决于您的代码结构,但由于您使用的是片段,我最好的猜测是您应该在Fragment中进行。

In the onResponseOK of the Retrofit call you will have something like this:在 Retrofit 调用的onResponseOK中,您将看到类似这样的内容:

@Override
public void onResponseOK(Response<List<Atencion>> response) {
    ...
    //access data here
}

In the callback you get your list.callback中你得到你的列表。 Pass the list to the adapter of your (I suppose) Recycler/Listview.将列表传递给您的(我想) Recycler/Listview. Access the list like this:像这样访问列表:

List<Atencion> myList = response.body();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM