0
장고 Rest Framework에서 웹 서비스에 첨부 된 이미지가있는 데이터베이스 객체를 보내려고하고 있으며 일부 발행물을 읽을 때 반드시 보내야한다는 것을 발견했습니다. API 서비스의 다중 부분 끝점을 통해데이터 및 이미지가있는 객체를 DRF 서비스에 추가하여 개조하는 방법
@Multipart
@POST("api/sincro_establecimiento/")
Call<Establecimiento> sincroEstablecimiento(
@Header("Authorization") String token,
@Part MultipartBody.Part foto,
@Part("json") RequestBody establecimiento
);
이 모델 클래스입니다 : :하지만 ... 내 서비스 엔드 포인트 그 방법이
그것을 할 수있는 올바른 방법을 찾을 수 없습니다
public class Establecimiento {
@SerializedName("id")
private Long id;
@SerializedName("nombre")
private String nombre;
@SerializedName("numero")
private String nro;
@SerializedName("posLatitud")
private String posLatitud;
@SerializedName("posLongitud")
private String posLongitud;
@SerializedName("foto")
private String foto;
@SerializedName("regimenTenencia")
private int regimenTenenciaId;
@SerializedName("regimenOtros")
private String regimenOtros;
public Establecimiento(Long id, String nombre, String nro, String posLatitud, String posLongitud, String foto, int regimenTenenciaId, String regimenOtros) {
this.id = id;
this.nombre = nombre;
this.nro = nro;
this.posLatitud = posLatitud;
this.posLongitud = posLongitud;
this.foto = foto;
this.regimenTenenciaId = regimenTenenciaId;
this.regimenOtros = regimenOtros;
}
public Establecimiento() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public String getNro() {
return nro;
}
public String getPosLatitud() {
return posLatitud;
}
public String getPosLongitud() {
return posLongitud;
}
public String getFoto() {
return foto;
}
public int getRegimenTenenciaId() {
return regimenTenenciaId;
}
public String getRegimenOtros() {
return regimenOtros;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public void setNro(String nro) {
this.nro = nro;
}
public void setPosLatitud(String posLatitud) {
this.posLatitud = posLatitud;
}
public void setPosLongitud(String posLongitud) {
this.posLongitud = posLongitud;
}
public void setFoto(String foto) {
this.foto = foto;
}
public void setRegimenTenenciaId(int regimenTenenciaId) {
this.regimenTenenciaId = regimenTenenciaId;
}
public void setRegimenOtros(String regimenOtros) {
this.regimenOtros = regimenOtros;
}
}
그리고 이것은 코드입니다
File foto = new File(element.getFoto());
final SharedPreferences prefs = getActivity().getApplication().getSharedPreferences("MDATUM_PREFS", Activity.MODE_PRIVATE);
RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), foto);
MultipartBody.Part body = MultipartBody.Part.createFormData("foto",foto.getName(),reqFile);
String establecimientoJson = new Gson().toJson(element);
Log.i("JSON GENERADO",establecimientoJson);
RequestBody establecimientoBody = RequestBody.create(MediaType.parse("multipart/form-data"),establecimientoJson);
Call<Establecimiento> establecimientoCall = webDatumApi.sincroEstablecimiento("Token "+prefs.getString("PREF_USER_TOKEN",null),body,establecimientoBody);
establecimientoCall.enqueue(new Callback<Establecimiento>() {
@Override
public void onResponse(Call<Establecimiento> call, Response<Establecimiento> response) {
Log.i("SUCCESS","Operacion con exito");
}
@Override
public void onFailure(Call<Establecimiento> call, Throwable t) {
Log.i("FAILURE","Operacion fallida");
}
});
서비스를 호출 할 때마다 다음과 같은 오류 메시지가 표시됩니다. 필드 nombre 및 필드 posLatitud는 필수이지만 필드는 json에서 전달됩니다.