因此,我正在Android Studio中创建一个简单的应用程序,该应用程序允许我按标题搜索电影并使用翻新连接到api,并获得响应,该响应返回包装在列表中的所有电影的列表。 RecycleView。这是API的示例:
http://www.omdbapi.com/?s=batman&plot=short&apikey=8fbbe1df
使用“ s”查询字符串,api将返回所有电影的对象数组,标题中带有该单词的单词如下:
这是模型类:
public class Movie {
private String Title;
private String Year;
private String imdbID;
private String Type;
private String Poster;
public Movie(String title, String year, String imdbID, String type, String poster) {
Title = title;
Year = year;
this.imdbID = imdbID;
Type = type;
Poster = poster;
}
public String getTitle() {
return Title;
}
public String getYear() {
return Year;
}
public String getImdbID() {
return imdbID;
}
public String getType() {
return Type;
}
public String getPoster() {
return Poster;
}
}
public class Movies {
private String totalResults;
private String Response;
private List<Movie> Search;
public Movies(String totalResults, String response, List<Movie> search) {
this.totalResults = totalResults;
Response = response;
Search = search;
}
public String getTotalResults() {
return totalResults;
}
public String getResponse() {
return Response;
}
public List<Movie> getSearch() {
return Search;
}
}
这是客户端实例:
public class RetrofitClientInstance {
private static Retrofit retrofit;
private static final String BASE_URL = "http://www.omdbapi.com";
public static Retrofit getRetrofitInstance() {
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
这是界面:
public interface GetDataService {
@GET("/?s=batman&plot=short&apikey=8fbbe1df")
Call<Movies> getAllMovies();
}
这是Main Activity中的回调方法:
private CustomAdapter adapter;
private RecyclerView recyclerView;
ProgressDialog progressDoalog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressDoalog = new ProgressDialog(MainActivity.this);
progressDoalog.setMessage("Loading....");
progressDoalog.show();
/*Create handle for the RetrofitInstance interface*/
GetDataService service = RetrofitClientInstance.getRetrofitInstance().create(GetDataService.class);
Call<Movies> call = service.getAllMovies();
call.enqueue(new Callback<Movies>() {
@Override
public void onResponse(Call<Movies> call, Response<Movies> response) {
progressDoalog.dismiss();
generateDataList(response.body().getSearch());
}
@Override
public void onFailure(Call<Movies> call, Throwable t) {
progressDoalog.dismiss();
Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
TextView error=(TextView) findViewById(R.id.textViewError);
error.setText(t.getMessage());
}
});
}
但是我想要一个编辑文本字段和一个按钮,当单击该按钮时会更改界面上的GET批注,该批注会更改api的's'查询值,以获取这些特定的影片,如下所示:
@GET("/?s="+value+"&plot=short&apikey=8fbbe1df")
Call<Movies> getAllMovies();
我有什么办法做到这一点,还是我做错了一切?
提前谢谢!