- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
构建一个新的 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/
我正在使用网络服务请求直方图数据。数据是数组中的一组数组: [[1375056000000,23.284713745117],[1375142400000,3.809531211853], [1375
这是我的 json 文件的相关部分: "categories": [ [ "Belgian Restaurant", "belgian" ],
我目前很困惑,为什么我不能在我的 Android 应用程序中从 JSONArray 中提取 JSONArray。下面给出了一个示例和我的源代码片段。 //The JSON { "cur
我正在尝试解析 JSONObject。 这个 JSONObject 中有一个 JSONArray,它在 JSONArray 中还有另一个 JSONArray。 我试图解析的 json 形式如下。 {
我正在尝试解析对象数据列表中的 JSON 数组 payment_details。 在这里,我已经成功解析了回收站 View 中调用的数据数组,但我无法调用 payment_details 数组。 适配
我正在寻找一个选项,可以将多个值添加到 JSONArray 并将其添加到另一个 JSONArray 中,而无需创建多个变量。例如: JSONArray array1 = new JSONArr
我遇到过一种情况,org.json.JSONArray 对象非常大,这最终会导致延迟和其他问题。因此,我们决定将 JSONArray 分割成更小的 block 。例如,如果 JSONArray 是这样
您好,我正在尝试在另一个 json 数组中解析一个 json 数组。帮我解决这个问题。 我的 Json 数组是 "results":[ { "percentcompleted"
我正在使用 Gson用于解析 json 响应。我需要在 JsonArray 中解析 JSONArry.. 我的回答是.. { "message": "Retreive sucessfully", "f
@GET @Produces("application/json") public Response GetAll() throws JSONException{ tblCategoryDao
我从服务器获取以下 json: { "Data": [ { "Record": [ " d11", "d12"
我正在使用 ajax 将 json 对象从 javascript 传递到 java servlet。 var jsonObj = JSON.stringify(objArray); //Then I
我需要获取作为“结果”内部数组的“示例”数据 JSON FILE 并将其附加到字符串。最简单的方法是什么? 这就是我得到“定义”的方式 private JSONObject queryResults;
我需要从 JSONArray 获取 JSONArray: JSONParser parser = new JSONParser(); JSONObject jObject=(J
我有一个 jsonArray 响应。我需要阅读此响应,其中包含 3 个 jsonArray。 这是 json 响应。这是一个 GET 请求并通过 Volley 请求发送。 [ "0x9000",
在此 Json 响应中,如何使用 Java 访问“smallImageUrls”并获取图像 url? 似乎“smallImageUrls”是“匹配”jsonarray 中的一个数组。如果我错了,请有人
这个问题在这里已经有了答案: Convert string to JSON array (10 个答案) 关闭 5 年前。 如何正确地将此字符串转换为 jsonArray? { "myArr
是否可以从较新的 EJB JSON 库转换为较旧的 org.json 库,而无需依赖 org.json.simple 或 GSON? 在下面的例子中,“buttons”是一个填充的 JsonArr
如何从jsonarray获取jsonarray? 我试过的代码。 for(loop) List stringList = new ArrayLis
我目前是 Json 新手,遇到了一个问题。我搜索了很多但找不到答案! 我从 json url 获取名称列表。名称可以在此 json 文件中重复,但我只想将它们的一条记录保留到我称为“arr”的新数组中
我是一名优秀的程序员,十分优秀!