I created an app where data is loaded from server with the help of an API using Retrofit 2 . How can I fetch data from different API with the help of single API interface method?
If I request data from first URL then i should get data from first API, And if i request data from second URL then i should get data from second URL only. how can i do this with the help of a single API interface class.
This is my Retrofit
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;
}
}
and This is my API interface class where my api url is stored.
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();
}
This is my Activity and API interface method. Where I have to show details from 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();
}
}
Your answer is very helpful for me.
CodePudding user response:
You can do something like this
public interface APIInterfaces {
@GET
Call<List<VideoModels>> getVid(@Url String URL);
}
For every call, you just have to pass the URL to the same method.
you implementation will change to this
// 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();
}
});