0
Django Rest Frameworkで画像をWebサービスに添付してデータベースオブジェクトを送信しようとしていますが、一部の出版物を読むと、 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で渡されます。