[英]How to pass dynamic parameters to rest api in android when using viewmodel
I am fetching news details from News Api site. 我从新闻Api网站获取新闻详情。 I am using ViewModel architecture so that the device does not fetch the results when orientation changes. 我正在使用ViewModel架构,以便在方向更改时设备无法获取结果。
As per various turorials i am able to fetch the result to a recyclerview using retrofit and viewmodel buy giving static parameters as query to the rest api. 根据各种turorial,我可以使用retrofit和viewmodel购买静态参数作为查询到其余api,将结果提取到recyclerview。
private void loadTopHeadlines() {
ApiInterface apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<TopHeadlinesResponse> response = apiInterface.getTopHeadlines("in", 20, 1,
"api_key");
response.enqueue(new Callback<TopHeadlinesResponse>() {
@Override
public void onResponse(Call<TopHeadlinesResponse> call, Response<TopHeadlinesResponse> response) {
topHeadlinesResponse.setValue(response.body().getArticles());
}
@Override
public void onFailure(Call<TopHeadlinesResponse> call, Throwable t) {
}
});
}
As you can see in the ViewModel class that is created the method getTopHeadlines() uses static parameters. 正如您在创建的ViewModel类中看到的那样,方法getTopHeadlines()使用静态参数。 How do i change it to dynamic parameters. 如何将其更改为动态参数。
Static parameter 静态参数
Call<TopHeadlinesResponse> response = apiInterface.getTopHeadlines("in", 20, 1,
"api_key");
Dynamic parameter 动态参数
Call<TopHeadlinesResponse> response = apiInterface.getTopHeadlines(dynamic, dynamic, dynamic,
"api_key");
So basically your method should accept those parameters, eg: 所以基本上你的方法应该接受这些参数,例如:
private void loadHeadlines(String stringValue, int number, int otherNumer)
//...do some stuff
Call<TopHeadlinesResponse> response = apiInterface.getTopHeadlines(stringValue, number, otherNumber, "api_key");
//rest stays the same
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.