我有一個關于 android 和改造的問題,我希望你能幫助我或給我一個如何解決它的線索。我將帶有 json 的 post 請求發送到 php REST api。日期具有“yyyy-MM-dd hh:mm:ss”格式,因為資料將存盤在 mysql 資料庫中。InformeEnvio 物件被轉換為 json 并使用 gson 設定格式,但在 php 服務器中,日期以這種格式到達:“Oct 22, 2021 6:55:55 PM”,盡管 android 控制臺中顯示的 json 格式是這樣的:“ 2021-10-22 18:55:55"。還有一點,我向郵遞員提出了一個請求,json 字串和日期以正確的格式到達。這是改造后的請求代碼和 pojo 代碼。
謝謝。
@Headers({
"Accept: application/json",
"Content-Type: application/json"
})
Call<PostResponse> saveInformeEnvio(@Body InformeEnvio item);
public class InformeEnvio {
private Visita visita;
private InformeCompra informeCompra;
private List<InformeCompraDetalle> informeCompraDetalles;
private List<ImagenDetalle> imagenDetalles;
private List<ProductoExhibido> productosEx;
public Visita getVisita() {
return visita;
}
public void setVisita(Visita visita) {
this.visita = visita;
}
public InformeCompra getInformeCompra() {
return informeCompra;
}
public void setInformeCompra(InformeCompra informeCompra) {
this.informeCompra = informeCompra;
}
public List<InformeCompraDetalle> getInformeCompraDetalles() {
return informeCompraDetalles;
}
public void setInformeCompraDetalles(List<InformeCompraDetalle> informeCompraDetalles) {
this.informeCompraDetalles = informeCompraDetalles;
}
public List<ImagenDetalle> getImagenDetalles() {
return imagenDetalles;
}
public void setImagenDetalles(List<ImagenDetalle> imagenDetalles) {
this.imagenDetalles = imagenDetalles;
}
public List<ProductoExhibido> getProductosEx() {
return productosEx;
}
public void setProductosEx(List<ProductoExhibido> productosEx) {
this.productosEx = productosEx;
}
public String toJson(InformeEnvio informe) {
// this.inf_visitasIdlocal=informe.getVisitasId();
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd hh:mm:ss").create();
String informejson=gson.toJson(informe.informeCompra);
String JSON = gson.toJson(informe);
return JSON;
}
}
uj5u.com熱心網友回復:
我認為您必須在創建 RestAdapter 時配置它。您可以GsonConverter進行改造以格式化您的日期。像這樣的代碼應該可以作業:
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd hh:mm:ss")
.create();
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(API_BASE_URL)
.setConverter(new GsonConverter.create(gson))
.build();
根據您構建客戶端的方式,您必須使用不同的方法:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/404614.html
標籤:
