如何从多个 API Retrofit 2 获取数据?

How to get data from multiple API Retrofit 2?

我创建了一个应用程序,在 API 使用 Retrofit 2 的帮助下从服务器加载数据。如何借助单个 API 接口方法从不同的 API 获取数据?

如果我从第一个 URL 请求数据,那么我应该从第一个 API 获取数据,如果我从第二个 URL 请求数据,那么我应该从第二个 [=] 获取数据46=] 而已。我如何在单个 API 界面 class.

的帮助下完成此操作

这是我的改造

public class RetrofitInstance {
static {
    System.loadLibrary("keys");
}
public static native String getUrl();


private static Retrofit retrofit;
public static Retrofit getRetrofit() {
    if (retrofit == null) {
        retrofit = new Retrofit.Builder().baseUrl(getUrl()).addConverterFactory(GsonConverterFactory.create()).build();
    }
    return retrofit;
}

}

并且这是我的 API 接口 class 我的 api url 存储的地方。

public interface APIInterfaces {

@GET("first.php")
Call<List<VideoModels>> getFirstVid();

@GET("second.php")
Call<List<VideoModels>> getSecondVid();


@GET("third.php")
Call<List<VideoModels>> getThirdVid();


@GET("fourth.php")
Call<List<VideoModels>> getFourthVid();

@GET("fifth.php")
Call<List<VideoModels>> getFifthVid();
}

这是我的Activity和API接口方法。我必须在哪里显示 API.

的详细信息
public class TestFragment extends Fragment {
TestFragmentBinding binding;

ArrayList<VideoModels> list = new ArrayList<>();
APIInterfaces interfaces;
@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    
    binding = TestFragmentBinding.inflate(inflater, container, false);

    ThumbnailViewer adapter = new ThumbnailViewer(list,getContext());
    binding.mRecyclerView.setAdapter(adapter);
    
    GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2);
    binding.mRecyclerView.setLayoutManager(gridLayoutManager);

    //Setup Data from API to ArrayList.

    interfaces = RetrofitInstance.getRetrofit().create(APIInterfaces.class);

    // I want to Change url as request and show data from "getSecondVid()","getThirdVid()" with singple interface method. Is it possible? 
    interfaces.getFirstVid().enqueue(new Callback<List<VideoModels>>() {
        @SuppressLint("NotifyDataSetChanged")
        @Override
        public void onResponse(Call<List<VideoModels>> call, Response<List<VideoModels>> response) {
            list.clear();
            if (response.isSuccessful() && response !=null) {
                list.addAll(response.body());
                adapter.notifyDataSetChanged();
            }
            else {
                Toast.makeText(getContext(), "No Data Found", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<List<VideoModels>> call, Throwable t) {
            Toast.makeText(getContext(), "Unable to Connect", Toast.LENGTH_SHORT).show();
        }
    });


    return binding.getRoot();
}

}

你的回答对我很有帮助

你可以这样做

public interface APIInterfaces {
    @GET
    Call<List<VideoModels>> getVid(@Url String URL);
}

对于每个调用,您只需将 URL 传递给相同的方法。

您的实现将更改为此

// you can pass the URL for the first.php, second.php video etc.
    interfaces.getVid("your_url").enqueue(new Callback<List<VideoModels>>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onResponse(Call<List<VideoModels>> call, Response<List<VideoModels>> response) {
                list.clear();
                if (response.isSuccessful() && response !=null) {
                    list.addAll(response.body());
                    adapter.notifyDataSetChanged();
                }
                else {
                    Toast.makeText(getContext(), "No Data Found", Toast.LENGTH_SHORT).show();
                }
            }
    
            @Override
            public void onFailure(Call<List<VideoModels>> call, Throwable t) {
                Toast.makeText(getContext(), "Unable to Connect", Toast.LENGTH_SHORT).show();
            }
        });