gpt4 book ai didi

java - 哪些参数最适合在 android studio 上发布包含 JSONArray 的 JSONObjetc?

转载 作者:太空宇宙 更新时间:2023-11-04 11:46:57 25 4
gpt4 key购买 nike

构建一个新的 Android 项目,已尝试使用改造从 woocoomerce REST API Web 服务将数据写入 JSON 中。

这些是用于包含要发送的数据的 POJO 类,请注意 lineitem 包含在 Pedido 内:

public class Pedido {

@SerializedName("id")
@Expose
private int id;
@SerializedName("status")
@Expose
private String status;
@SerializedName("date_created")
@Expose
private String dateCreated;
private String dateModified;
private int customerId;
private String total;
@SerializedName("customer_note")
@Expose
private String customerNote;
private String dateCompleted;
private String datePaid;
@SerializedName("line_items")
@Expose
private ArrayList<LineItem> line_items;


public int getId() {
return id;
}


public void setId(int id) {
this.id = id;
}


public String getStatus() {
return status;
}


public void setStatus(String status) {
this.status = status;
}


public String getDateCreated() {
return dateCreated;
}


public void setDateCreated(String dateCreated) {
this.dateCreated = dateCreated;
}


public String getDateModified() {
return dateModified;
}


public void setDateModified(String dateModified) {
this.dateModified = dateModified;
}


public int getCustomerId() {
return customerId;
}


public void setCustomerId(int customerId) {
this.customerId = customerId;
}


public String getTotal() {
return total;
}


public void setTotal(String total) {
this.total = total;
}


public String getCustomerNote() {
return customerNote;
}


public void setCustomerNote(String customerNote) {
this.customerNote = customerNote;
}


public String getDateCompleted() {
return dateCompleted;
}


public void setDateCompleted(String dateCompleted) {
this.dateCompleted = dateCompleted;
}


public String getDatePaid() {
return datePaid;
}


public void setDatePaid(String datePaid) {
this.datePaid = datePaid;
}


public List<LineItem> getLineItems() {
return line_items;
}


public void setLineItems(ArrayList<LineItem> line_items) {
this.line_items = line_items;
}

}

public class LineItem {

@SerializedName("id")
@Expose
private int id;
private String itemname;
@SerializedName("quantity")
@Expose
private int quantity;
private double price;
private String total;

public int getidprod(){return id;}

public void setIdprod(int id){this.id = id;}

public String getitemname(){return itemname;}

public int getQuantity(){return quantity;}

public void setQuantity(int quantity){this.quantity = quantity;}

public double getPrice(){return price;}

public String gettotal(){return total;}

}

接下来,用于 POST 数据的 Endpoint 方法(这就是问题所在),它仅创建空寄存器

@POST("orders"+ ConstantesAPI.ACCES_TOKEN)
Call<Pedido> Crearpedido(@Body Pedido pedido);

最后,这是方法实例的调用方式:

pedido.setCustomerId(id_cliente_np);
pedido.setDateCreated(df.format(c.getTime()));
pedido.setLineItems(lineitems);

Call<Pedido> peticionpedido = pedidoapi.Crearpedido(pedido);
peticionpedido.enqueue(new Callback<Pedido>() {
@Override
public void onResponse(Call<Pedido> call, Response<Pedido> response) {
Toast.makeText(getApplicationContext(), "Pedido de cliente: "+ pedido.getcustomerId() +" Realizado con Éxito", Toast.LENGTH_LONG).show();
}

@Override
public void onFailure(Call<Pedido> call, Throwable t) {
Toast.makeText(getApplicationContext(), "Error: el Pedido no pudo ser realizado", Toast.LENGTH_LONG).show();
}
});

JSON 响应如下,其中“customer_id”为 0 并且未发布“line_items”:

{

"id": 261,
"parent_id": 0,
"status": "pending",
"order_key": "wc_order_58a26745d6579",
"number": 261,
"currency": "MXN",
"version": "2.6.7",
"prices_include_tax": false,
"date_created": "2017-02-14T02:11:17",
"date_modified": "2017-02-14T02:11:17",
"customer_id": 0,
"discount_total": "0.00",
"discount_tax": "0.00",
"shipping_total": "0.00",
"shipping_tax": "0.00",
"cart_tax": "0.00",
"total": "0.00",
"total_tax": "0.00",
"billing": {
"first_name": "",
"last_name": "",
"company": "",
"address_1": "",
"address_2": "",
"city": "",
"state": "",
"postcode": "",
"country": "",
"email": "",
"phone": ""
},
"shipping": {
"first_name": "",
"last_name": "",
"company": "",
"address_1": "",
"address_2": "",
"city": "",
"state": "",
"postcode": "",
"country": ""
},
"payment_method": "",
"payment_method_title": "",
"transaction_id": "",
"customer_ip_address": "187.167.213.178",
"customer_user_agent": "okhttp/3.3.0",
"created_via": "rest-api",
"customer_note": "",
"date_completed": "2017-02-14T02:11:17",
"date_paid": "",
"cart_hash": "",
"line_items": [ ],
"tax_lines": [ ],
"shipping_lines": [ ],
"fee_lines": [ ],
"coupon_lines": [ ],
"refunds": [ ],

}

像这样更改端点的方法参数:

@FormUrlEncoded
@POST("orders"+ ConstantesAPI.ACCES_TOKEN)
Call<Pedido> Crearpedido(
@Field("date_created")String date,
@Field("customer_id") int idcliente,
@Field("line_items[]") ArrayList<LineItem> line_items);


EndpointsAPI pedidoapi = apiadapter.conectarRestAPI(gsonBuilder.create());
Call<Pedido> peticionpedido = pedidoapi.Crearpedido(pedido.getDateCreated(),pedido.getCustomerId(), lineitems);
peticionpedido.enqueue(new Callback<Pedido>() {
@Override
public void onResponse(Call<Pedido> call, Response<Pedido> response) {
Toast.makeText(getApplicationContext(), "Pedido de cliente: "+ id_cliente_np +" Realizado con Éxito", Toast.LENGTH_LONG).show();
}

@Override
public void onFailure(Call<Pedido> call, Throwable t) {
Toast.makeText(getApplicationContext(), "Error: el Pedido no pudo ser realizado", Toast.LENGTH_LONG).show();
}
});

JSON 响应正确发布了“customer_id”字段 (5),但仍然缺少“line_items:”字段

{

"id": 279,
"parent_id": 0,
"status": "pending",
"order_key": "wc_order_58a3c50135a4f",
"number": 279,
"currency": "MXN",
"version": "2.6.7",
"prices_include_tax": false,
"date_created": "2017-02-15T03:03:29",
"date_modified": "2017-02-15T03:03:29",
"customer_id": 5,
"discount_total": "0.00",
"discount_tax": "0.00",
"shipping_total": "0.00",
"shipping_tax": "0.00",
"cart_tax": "0.00",
"total": "0.00",
"total_tax": "0.00",
"billing": {
"first_name": "",
"last_name": "",
"company": "",
"address_1": "",
"address_2": "",
"city": "",
"state": "",
"postcode": "",
"country": "",
"email": "eugenio_atocpan@panaderiaelrollo.com.mx",
"phone": ""
},
"shipping": {
"first_name": "",
"last_name": "",
"company": "",
"address_1": "",
"address_2": "",
"city": "",
"state": "",
"postcode": "",
"country": ""
},
"payment_method": "",
"payment_method_title": "",
"transaction_id": "",
"customer_ip_address": "187.167.213.178",
"customer_user_agent": "okhttp/3.3.0",
"created_via": "rest-api",
"customer_note": "",
"date_completed": "2017-02-15T03:03:29",
"date_paid": "",
"cart_hash": "",
"line_items": [ ],
"tax_lines": [ ],
"shipping_lines": [ ],
"fee_lines": [ ],
"coupon_lines": [ ],
"refunds": [ ],

}

非常感谢!

最佳答案

你在服务器端使用相同的GSON吗?如前所述,这可能是因为以下字段:

@SerializedName("line_items")
@Expose
private ArrayList<LineItem> line_items;

因为您使用了 SerializedName,所以此列表的 JSON 序列化将为:

{“line_items”:[..]}

当这个 JSON 到达服务器端时,服务器端可能不会反序列化这个字段,因为它有点不遵循约定。

要解决此问题,您可以:1)将@SerializedName(“line_items”)更改为@SerializedName(“lineItems”)或

2) 将 line_items 更改为 lineItems 以遵循约定并删除 @SerializedName("line_items") 或

3)更改服务器代码以指示它将“line_items”字段反序列化为对象属性。(如果服务器端是Java并且您在服务器端使用Jackson,请将其添加到line_items对象:@JsonProperty(“line_items”)。

关于java - 哪些参数最适合在 android studio 上发布包含 JSONArray 的 JSONObjetc?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42260435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com