Java 在android studio上发布包含经过改装的JSONArray的JSONOBJECT时,哪些参数是最好的?

Java 在android studio上发布包含经过改装的JSONArray的JSONOBJECT时,哪些参数是最好的?,java,android,json,retrofit2,woocommerce-rest-api,Java,Android,Json,Retrofit2,Woocommerce Rest Api,在构建一个新的android项目时,它曾尝试使用改型将数据写入来自woocoomerce REST API web服务的JSON中 这些是用于包含要发送的数据的POJO类,请注意,lineitem包含在Pedido中: public class Pedido { @SerializedName("id") @Expose private int id; @SerializedName("status") @Expose private String status; @SerializedNam

在构建一个新的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;
}
}

接下来是用于发布数据的端点方法(这里是问题所在),它只创建空寄存器

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

如下所示更改端点的方法参数:

@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();
        }
    });
}


非常感谢

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

@SerializedName("line_items")
@Expose
private ArrayList<LineItem> line_items;
当这个JSON到达服务器端时,服务器端可能不会反序列化这个字段,因为它有点不符合约定

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

2) 将行项目更改为行项目以遵循约定并删除@SerializedName(“行项目”)或


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

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

@SerializedName("line_items")
@Expose
private ArrayList<LineItem> line_items;
当这个JSON到达服务器端时,服务器端可能不会反序列化这个字段,因为它有点不符合约定

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

2) 将行项目更改为行项目以遵循约定并删除@SerializedName(“行项目”)或


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

如果您创建的是空寄存器,端点就可以了。我从来没有使用过Reformation2,所以很难说,但您应该在服务器端记录您的请求,并查看数据是如何发送的。您可能发送的数据不太好。您还应该在Android中发送对象之前调试对象Pedido,以查看其格式是否正确。问题更新@JuliatzindelToro,谢谢你,如果你创建的是空寄存器,端点就可以了。我从来没有使用过Reformation2,所以很难说,但是你应该在服务器端记录你的请求,看看数据是如何发送的。你可能发送的数据不好。你还应该在Android中发送对象之前调试对象Pedido,看看它是否格式正确。问题已更新@JuliatzindelToro,非常感谢您的回答@NangSaigon,尝试将第一个建议代码编辑为:
@SerializedName(“lineitems”)@Expose private ArrayList line_items;public ArrayList getLineItems(){return line_items;}public void setLineItems(ArrayList line_items){this.line_items=line_items;}
但仍能获得相同的结果也尝试将代码更改为:
private ArrayList lineitems;
但仍然只发布客户的idlineItems,而不是lineitems。请参阅POJO约定的此链接:好的,很好,您是指POJO类吗?非常感谢!我应该全部阅读吗,还是它是你会得到更多的奖励吗?再次非常感谢!非常感谢你的回答@NangSaigon,尝试将第一个建议代码编辑为:
@SerializedName(“lineitems”)@Expose private arraraylist line_items;public arrarylist getLineItems(){return line_items;}public void setLineItems(ArrayList line_items){this.line_items=line_items;}
但仍能获得相同的结果也尝试将代码更改为:
private ArrayList lineitems;
但仍然只发布客户的idlineItems,而不是lineitems。请参阅POJO约定的此链接:好的,很好,您是指POJO类吗?非常感谢!我应该全部阅读吗,还是它是你会得到更多的报酬吗?再次非常感谢!
@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();
        }
    });
{

"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": [ ],
@SerializedName("line_items")
@Expose
private ArrayList<LineItem> line_items;
{“line_items”:[..]}