如何使用改造发布长JSON作为一个身体?

时间:2017-10-24 06:49:00

标签: android json post retrofit

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

  

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

我需要将post params转换为以下格式。

身体:

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

4 个答案:

答案 0 :(得分:0)

尝试使用@Body注释。

创建一个User类将您的数据添加到该实例并使用改进而不是使用@field,您应该发送@Body作为正文的User类。

示例:

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

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

答案 1 :(得分:0)

改造过于典型,无法实施,这是我用于改造的最简单的方法。

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(&#34; Content-Type&#34;)String contentType 在api服务中很有用,只需使用@Field发送请求

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

答案 2 :(得分:0)

必须使用@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);

答案 3 :(得分:0)

感谢帮助伙伴,我使用@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;
}

}