【问题标题】:How to change the value only in specific parameter in retrofit android如何仅在改造android中的特定参数中更改值
【发布时间】:2019-07-08 03:34:31
【问题描述】:

我有网址

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 传递,它正在工作,但我需要将其更改为输入参数。

【问题讨论】:

    标签: android android-recyclerview gson retrofit2


    【解决方案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);
    }
    

    创建模型类 Pixabay

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

    }

    创建类 Pixabay响应

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

    【讨论】:

    • 我收到 404 响应
    【解决方案2】:

    你可以做类似的事情。

     public interface ImageApiInterface {
        @GET("?key=12973823-c1d0c689a2cb0af7706951221&q={query}&image_type=photo")
        Call<MyImages> getMovieDetails(@Path("query") String query);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      • 2011-01-04
      相关资源
      最近更新 更多