如何在翻新中传递json主体的请求参数?

时间:2019-05-03 05:00:41

标签: android gson retrofit2

如何在MainActivity的呼叫中为json类型参数设置请求参数?

这是我的Interface类,其中有类型为application / json的标头批注并使用post方法,并在其中使用具有用于参数请求模型的正文批注的调用

public interface ApiStorePhotoInterface {
        @Headers("Content-Type: application/json")
        @POST("photo/st/v7")
        Call<PhotoStoreMainModel> getResponse(@Body ParamModel paramModel);
    }

这里的ParamModel是json模型,可用于调用。

2个用于@Body的参数模型- 该模型包含带有List的值,其中ParamProductDetailModel是其中的第二个模型。    第一个参数模型-

public class ParamModel {

    @SerializedName("apiKey")
    @Expose
    private String apiKey;
    @SerializedName("affId")
    @Expose
    private String affId;
    @SerializedName("act")
    @Expose
    private String act;
    @SerializedName("latitude")
    @Expose
    private String latitude;
    @SerializedName("longitude")
    @Expose
    private String longitude;
    @SerializedName("devinf")
    @Expose
    private String devinf;
    @SerializedName("appver")
    @Expose
    private String appver;
    @SerializedName("productDetails")
    @Expose
    private List<ParamProductDetailModel> productDetails = null;

    public String getApiKey() {
        return apiKey;
    }

    public void setApiKey(String apiKey) {
        this.apiKey = apiKey;
    }

    public String getAffId() {
        return affId;
    }

    public void setAffId(String affId) {
        this.affId = affId;
    }

    public String getAct() {
        return act;
    }

    public void setAct(String act) {
        this.act = act;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getDevinf() {
        return devinf;
    }

    public void setDevinf(String devinf) {
        this.devinf = devinf;
    }

    public String getAppver() {
        return appver;
    }

    public void setAppver(String appver) {
        this.appver = appver;
    }

    public List<ParamProductDetailModel> getProductDetails() {
        return productDetails;
    }

    public void setProductDetails(List<ParamProductDetailModel> productDetails) {
        this.productDetails = productDetails;
    }

}

第二个参数模型-

 public class ParamProductDetailModel  {

    @SerializedName("productId")
    @Expose
    private String productId;
    @SerializedName("qty")
    @Expose
    private String qty;

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public String getQty() {
        return qty;
    }

    public void setQty(String qty) {
        this.qty = qty;
    }

}

这是客户端类

客户端类-

这是改造生成器类

public class ApiStorePhotoClient {

    private static final String BASE_URL = "  https://services.something.com/api/";

    private static ApiStorePhotoInterface apiInterface;

    public static ApiStorePhotoInterface getApi() {
        if (apiInterface == null) {
            apiInterface = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()
                    .create(ApiStorePhotoInterface.class);
        }
        return apiInterface;
    }
}

现在,在我的主班上-

ApiStorePhotoInterface apiInterface = ApiStorePhotoClient.getApi();

Call<PhotoStoreMainModel> call = apiInterface.getResponse("What to write here??");

如何在getResponse()中的模型内发送请求?

PhotoStoreMainModel是响应模型。该代码具有3个响应模型,其中PhotoStoreMainModel是其中之一,而2个请求参数模型已在上面发布。

如何在getResponse()的Mainclass中的@body ParamModel中发送请求参数?

如何在MainActivity类中为呼叫设置线路?

  

我期望结果,但由于请求参数而无法访问API

2 个答案:

答案 0 :(得分:1)

您可以像这样添加arrayObjects,更新后可以使用Log检查您的请求。

        JsonObject jsonObject1 = new JsonObject();
        jsonObject1.addProperty("apiKey", "wseoagbiis5dd6vq5tfbs3r1c8rsz");
        jsonObject1.addProperty("appver", "1.00");
        jsonObject1.addProperty("affid", "team");

        Global.printLog("jsonObject1", "===" + jsonObject1);

        JsonArray productDetail1Array = new JsonArray();

        for (int i1 = 0; i1 < 1; i1++) {
            JsonObject productDetail1 = new JsonObject();
            productDetail1.addProperty("productId", "6670002");

            JsonArray array1 = new JsonArray();

            for (int j = 0; j < 2; j++) {
                JsonObject imageDetails = new JsonObject();

                imageDetails.addProperty("qty", j + "");
                imageDetails.addProperty("url", "something.com/assets_admin/uploads/frame_image/…");
                array1.add(imageDetails);
            }

            productDetail1.add("imageDetails", array1);

            productDetail1Array.add(productDetail1);

        }
        jsonObject1.add("productDetails", productDetail1Array);

        Global.printLog("jsonObject1", "===" + jsonObject1);

您可以像这样传递对象:

 JSONObject jsonObject = new JSONObject();
   try {
     jsonObject.put("key1", "value1");
     jsonObject.put("key2", value2);

     JsonParser jsonParser = new JsonParser();

    ApiStorePhotoInterface apiInterface = ApiStorePhotoClient.getApi();

    **Call<PhotoStoreMainModel> call = apiInterface.getResponse((JsonObject) jsonParser.parse(jsonObject.toString()));  //This is not the correct way to parse the json. Not getting correct hit** 

    call.enqueue(new Callback<PhotoStoreMainModel>() {
                @Override
                public void onResponse(Call<PhotoStoreMainModel> call, Response<PhotoStoreMainModel> response) {
                 }

                @Override
                public void onFailure(Call<PhotoStoreMainModel> call, Throwable t) {
                    call.cancel();
                    t.printStackTrace();
                }
            });

    } catch (JSONException e) {
            e.printStackTrace();
    }

答案 1 :(得分:1)

无需添加content type

更改您的interface样式

public interface ApiStorePhotoInterface {


        @POST("photo/st/v7")
        Call<PhotoStoreMainModel> getResponse(@Body JsonObject obj);
    }

使json object像这样:

JsonObject json = new JsonObject();
json.addProperty("affId", id_value)

//就像所有参数一样

在getResponse中的对象中传递以上内容