我有网址
https://pixabay.com/api/?key=12973823-c1d0c689a2cb0af7706951221&q=dogs&image_type=photo
在上面的URL中,q = query这是我需要更改数据以传递参数的地方。
但是我不知道如何仅在q中传递参数,而q之后的所有其他参数都是固定的。
我只能以静态方式申请,但我不知道如何通过将edittext字段作为字符串传递给该参数q =来更改q
public class RetrofitService {
private static Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://pixabay.com/api/")
.addConverterFactory(GsonConverterFactory.create())
.build();
//can't change to private. Since the use case of create service is in another class
public static <S> S createService(Class<S> serviceClass) {
return retrofit.create(serviceClass);
}
}
public interface ImageApiInterface {
@GET("?key=12973823-c1d0c689a2cb0af7706951221&q=dogs&image_type=photo")
Call<MyImages> getMovieDetails();
}
这里我将静态字符串作为dog传递,但是可以,但是我需要将其更改为输入参数。
答案 0 :(得分:1)
您的API接口将如下所示:
public interface ImageApiInterface {
String PIXABAY_URL = "https://pixabay.com/";
@GET("api/?key=12973823-c1d0c689a2cb0af7706951221&image_type=photo")
Call<PixabayResponse> getMovieDetails(@Query("q") String query);
}
创建模型类PID
public class Pixabay implements Serializable {
@SerializedName("id")
@Expose
private String id;
@SerializedName("largeImageURL")
@Expose
private String imageUrl;
@SerializedName("user_id")
@Expose
private String userId;
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}
创建类别PixelResponse
public class PixabayResponse implements Serializable {
@SerializedName("totalHits")
@Expose
private String totalHits;
@SerializedName("hits")
@Expose
private List<Pixabay> pixabayList;
public List<Pixabay> getPixabayList() {
return pixabayList;
}
public void setPixabayList(List<Pixabay> pixabayList) {
this.pixabayList = pixabayList;
}
public String getTotalHits() {
return totalHits;
}
public void setTotalHits(String totalHits) {
this.totalHits = totalHits;
}
}
现在在您的活动中创建一个方法:
private void callPixabayImages(String query){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.PIXABAY_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
Api api = retrofit.create(Api.class);
Call<PixabayResponse> call = api.getMovieDetails(query);
call.enqueue(new Callback<PixabayResponse>() {
@Override
public void onResponse(Call<PixabayResponse> call, Response<PixabayResponse> response) {
List<Pixabay> imagesList = response.body().getPixabayList();
for (int i=0; i<imagesList.size(); i++){
Pixabay data = imagesList.get(i);
System.out.println("ID# "+ data.getId());
System.out.println("UserID: "+ data.getUserId());
System.out.println("ImageURL: "+ data.getImageUrl());
}
}
@Override
public void onFailure(Call<PixabayResponse> call, Throwable t) {
System.out.println("Error: "+ t.getMessage());
}
});
}
现在按如下所示调用此方法:
callPixabayImages("dogs");
答案 1 :(得分:0)
您可以执行类似的操作。
public interface ImageApiInterface {
@GET("?key=12973823-c1d0c689a2cb0af7706951221&q={query}&image_type=photo")
Call<MyImages> getMovieDetails(@Path("query") String query);
}