我創建了一個應用程式,在 API 的幫助下使用 Retrofit 2 從服務器加載資料。如何借助單一 API 介面方法從不同的 API 獲取資料?
如果我從第一個 URL 請求資料,那么我應該從第一個 API 獲取資料,如果我從第二個 URL 請求資料,那么我應該只從第二個 URL 獲取資料。我如何在單個 API 介面類的幫助下做到這一點。
這是我的改造
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 介面類,其中存盤了我的 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();
}
}
你的回答對我很有幫助。
uj5u.com熱心網友回復:
你可以做這樣的事情
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();
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/455639.html
上一篇:NestJSHTTPModule需要在每個功能模塊中匯入
下一篇:(Express、Node、Typescript、RESTAPI)如何將服務中的函式匯入控制器?屬性...在typeof型別上不存在
