[英]How to post long JSON as a body using retrofit?
I am using retrofit to handle the API calls but facing problem while posting long JSON. 我正在使用翻新来处理API调用,但是在发布长JSON时遇到了问题。 I am getting:
我正进入(状态:
internal server error(code : 500)
内部服务器错误(代码:500)
I need to convert post params to below format. 我需要将post params转换为以下格式。
Body : 身体 :
{"UserId" : "2",
"PortalId" : "1",
"LocaleId" : "1",
"CatalogId" : "3",
"Items" : [{"Name" : "ap1234","Quantity" : "1"}]}
Below is the code I am using Api Call : 以下是我使用Api Call的代码:
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>() {
Retrofit Interface : 改造接口:
@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);
Try using the @Body annotation . 尝试使用@Body批注。
Create a User class add your data to that instance and with retrofit instead of using @field you should send @Body with the User class as body . 创建一个User类,将您的数据添加到该实例中,并进行改造,而不是使用@field,您应该发送@Body并将User类作为body。
example: 例:
interface Foo {
@POST("/jayson")
FooResponse postRawJson(@Body TypedInput body);
}
For more info I found this link to be helpful https://futurestud.io/tutorials/retrofit-send-objects-in-request-body 有关更多信息,我发现此链接很有用https://futurestud.io/tutorials/retrofit-send-objects-in-request-body
retrofit is too typical to implement and this is the easiest method I ever used for retrofit. 改造太典型了,无法实现,这是我有史以来最简单的改造方法。
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();
}
}
}
});
}
I don't think @Header("Content-Type") String contentType is useful in api services, just use @Field to send request 我不认为@Header(“ Content-Type”)字符串contentType在api服务中很有用,只需使用@Field发送请求
@POST("list_data")
@FormUrlEncoded
Call<JsonObject> shopList(@Field("string") String string);
@Body String body has to be used. @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>() {
change as following 改变如下
@FormUrlEncoded
@POST(API_ADD_TO_CART)
Call<DataResponse> getData(@Header("Content-Type") String contentType, @Body String body);
Thanks for the help guys I did it using @Body 感谢您使用@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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.