如何使用 retrofit2 创建 json 对象数组的查询

How to create a query of array of json objects with retrofit2

我需要使用 retrofit2 生成以下查询

s=[{"uuid":"f7826da6-4fa2-4e98-8024-bc5b71e0893e","major":29551,"minor":30826}]

我尝试了几种方法,包括:

@Query("s[uuid]") String uuid,
@Query("s[major]") int major,
@Query("s[minor]") int minor

但是我无法使用 retrofit2 正确创建查询,生成我想要的查询的正确方法是什么?

一步一步做 Steve.First 将您的 JSON 数组转换为 Pojo.I 已为您完成此检查:

public class BeaconPojo {







        @SerializedName("uuid")
        @Expose
        private String uuid;
        @SerializedName("major")
        @Expose
        private Integer major;
        @SerializedName("minor")
        @Expose
        private Integer minor;

        /**
         *
         * @return
         * The uuid
         */
        public String getUuid() {
            return uuid;
        }

        /**
         *
         * @param uuid
         * The uuid
         */
        public void setUuid(String uuid) {
            this.uuid = uuid;
        }

        /**
         *
         * @return
         * The major
         */
        public Integer getMajor() {
            return major;
        }

        /**
         *
         * @param major
         * The major
         */
        public void setMajor(Integer major) {
            this.major = major;
        }

        /**
         *
         * @return
         * The minor
         */
        public Integer getMinor() {
            return minor;
        }

        /**
         *
         * @param minor
         * The minor
         */
        public void setMinor(Integer minor) {
            this.minor = minor;
        }

    }

然后像这样创建一个界面:

public class EndPointInterface {
    @GET("getBecon")
    Call<ArrayList<BeaconPojo>> getBeacons();
}

之后创建实用程序 class:

public class ApiUtil {

 public static final boolean isEnableLogging = true;


    public static Retrofit getPetListInstance(){
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        if(isEnableLogging)
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        else
            logging.setLevel(HttpLoggingInterceptor.Level.NONE);
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(logging);
        return new Retrofit.Builder()
                .baseUrl(Constant.base_url)
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient.build())
                .build();

    }





}

然后当你调用网络服务时试试这个:

ApiEndInterface service = 

    ApiUtil.getPetListInstance().create(ApiEndInterface.class);
            Call<ArrayList<BeaconPojo>> call = service.getPets();
            call.enqueue(new Callback<ArrayList<BeaconPojo>() {
                @Override
                public void onResponse(Call<ArrayList<BeaconPojo>> call, Response<ArrayList<BeaconPojo>> response) {

                }

                @Override
                public void onFailure(Call<ArrayList<BeaconPojo>> call, Throwable t) {

                }
            });

您的查询参数是 s,因此您需要这样的参数:

@Query("s") MyParamType s

一个简单的解决方案是声明具有属性(uuidmajorminor)的 class(例如 MyParamType)并覆盖toString() 方法 return 所需的格式。

示例:

public String toString() {
  return String.format("[{\"uuid\":\"%s\",\"major\":%d,\"minor\":%d}]", this.uudi, this.major, this.minor);
}