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

时间:2016-05-05 12:28:00

标签: android json retrofit 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正确创建查询,生成我想要的查询的正确方法是什么?

2 个答案:

答案 0 :(得分:1)

一步一步地执行Steve.First将您的JSON数组转换为Pojo。我已经为您做了这个检查:

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

之后创建一个Utilility类:

公共类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) {

                }
            });

答案 1 :(得分:1)

您的查询参数是s,因此您需要以下内容:

@Query("s") MyParamType s

一个简单的解决方案是使用属性(MyParamTypeuuidmajor)声明一个类(例如minor)并覆盖toString()方法返回所需的格式。

示例:

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