【问题标题】:How to post long JSON as a body using retrofit?如何使用改造将长 JSON 作为正文发布?
【发布时间】:2017-10-24 06:48:59
【问题描述】:

我正在使用 retrofit 处理 API 调用,但在发布长 JSON 时遇到问题。我得到:

内部服务器错误(代码:500)

我需要将帖子参数转换为以下格式。

身体:

{"UserId" : "2",
"PortalId" : "1",
"LocaleId" : "1",
"CatalogId" : "3",
"Items" : [{"Name" : "ap1234","Quantity" : "1"}]}

下面是我正在使用的代码 API 调用:

JSONArray array = new JSONArray();
        try {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("Name", "ap1234");
            jsonObject1.put("Quantity", "1");
            array.put(jsonObject1);
        } catch (JSONException e) {
            e.printStackTrace();
        }

Call data = mApiInterface.getData("application/json","2", "1", "1", "3", array.toString());
        addToCart.enqueue(new Callback<DataResponse>() {

改造界面:

@FormUrlEncoded
    @POST(API_ADD_TO_CART)
    Call<DataResponse> getData(@Header("Content-Type") String contentType, @Field("UserId") String userId,
                               @Field("LocaleId") String localeId,
                               @Field("PortalId") String portalId,
                               @Field("CatalogId") String CatalogId,
                               @Field("Items") String Items);

【问题讨论】:

    标签: android json post retrofit


    【解决方案1】:

    @Body String body必须使用。

    JSONArray array = new JSONArray();
        try {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("Name", "ap1234");
            jsonObject1.put("Quantity", "1");
    /* 
     create a json object pass as body   
     {"UserId" : "2",
       "PortalId" : "1",
        "LocaleId" : "1", 
        "CatalogId" : "3",
        "Items" : [{"Name" : "ap1234","Quantity" : "1"}]}
    
           */
        array.put(jsonObject1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
      Call data = mApiInterface.getData("application/json","2", "1", "1", 
    "3", array.toString());
        addToCart.enqueue(new Callback<DataResponse>() {
    

    如下修改

    @FormUrlEncoded
    @POST(API_ADD_TO_CART)
    Call<DataResponse> getData(@Header("Content-Type") String contentType, @Body String body);
    

    【讨论】:

      【解决方案2】:

      尝试使用@Body 注解。

      创建一个 User 类,将您的数据添加到该实例中,并且通过改造而不是使用 @field,您应该发送 @Body 并将 User 类作为 body 。

      示例:

      interface Foo {
        @POST("/jayson")
        FooResponse postRawJson(@Body TypedInput body);
      }
      

      有关更多信息,我发现此链接很有帮助 https://futurestud.io/tutorials/retrofit-send-objects-in-request-body

      【讨论】:

        【解决方案3】:

        改造太典型了,无法实施,这是我用来改造的最简单的方法。

        public void sendPost(String s1) {
                dialog.setMessage("Please wait...");
                dialog.setCancelable(false);
                dialog.show();
        
                apiService.getData(s1).enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
        
                        dialog.dismiss();
                        try {
                            if (response != null) {
                                JSONObject jsonObject = new JSONObject(response.body().toString());
                                String status = jsonObject.optString("status");
        
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
        
                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {
                        if (dialog != null) {
                            if (dialog.isShowing()) {
                                dialog.dismiss();
                            }
                        }
                    }
                });
            }
        

        我觉得@Header("Content-Type") String contentType在api服务中没什么用,直接用@Field发送请求就行了

        @POST("list_data")
        @FormUrlEncoded
        Call<JsonObject> shopList(@Field("string") String string);
        

        【讨论】:

        • 感谢我使用@Body 的帮助
        【解决方案4】:

        感谢我使用@Body 的帮助

        @POST(API_ADD_TO_CART)
        Call<ShoppingCartResponse> getData(@Header("Content-Type") String contentType, @Body DataRequest request)
        
        
        public class AddToCartRequest {
        
        @SerializedName("UserId")
        @Expose
        private String userId;
        @SerializedName("PortalId")
        @Expose
        private String portalId;
        @SerializedName("LocaleId")
        @Expose
        private String localeId;
        @SerializedName("CatalogId")
        @Expose
        private String catalogId;
        @SerializedName("Items")
        @Expose
        private List<Items> items = null;
        
        public String getUserId() {
        return userId;
        }
        
        public void setUserId(String userId) {
        this.userId = userId;
        }
        
        public String getPortalId() {
        return portalId;
        }
        
        public void setPortalId(String portalId) {
        this.portalId = portalId;
        }
        
        public String getLocaleId() {
        return localeId;
        }
        
        public void setLocaleId(String localeId) {
        this.localeId = localeId;
        }
        
        public String getCatalogId() {
        return catalogId;
        }
        
        public void setCatalogId(String catalogId) {
        this.catalogId = catalogId;
        }
        
        public List<Item> getItems() {
        return items;
        }
        
        public void setItems(List<Item> items) {
        this.items = items;
        }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-06-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-30
          • 1970-01-01
          相关资源
          最近更新 更多