首页 > 解决方案 > 如何仅在改造android中的特定参数中更改值

问题描述

我有网址

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();
}

在这里,我将静态字符串作为狗传递,它正在工作,但我需要将其更改为输入参数。

标签: androidandroid-recyclerviewgsonretrofit2

解决方案


您的 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);
}

创建模型类

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;
}

}

创建类pixabayResponse

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");

推荐阅读