2017-11-30 15 views
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で渡されます。

答えて

0

webserviceをデバッグして、 "json"と "foto"という2つの要素のみで構成されたオブジェクトを受信して​​いたので、サービスエンドポイントで次のように変更しました。

@Multipart 
    @POST("api/sincro_establecimiento/") 
    Call<Establecimiento> sincroEstablecimiento(
      @Header("Authorization") String token, 
      @Part MultipartBody.Part foto, 
      @Part("nombre") RequestBody nombre, 
      @Part("numero") RequestBody numero, 
      @Part("posLatitud") RequestBody posLatitud, 
      @Part("posLongitud") RequestBody posLongitud, 
      @Part("regimenTenencia") RequestBody regimenTenencia, 
      @Part("regimenOtros") RequestBody regimenOtros 

    ); 

一つ私が送信する必要があるフィールドごと@Part、また、フラグメントで次のように変更:

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); 
      RequestBody nombre = RequestBody.create(MediaType.parse("text/plain"),element.getNombre()); 
      RequestBody numero = RequestBody.create(MediaType.parse("text/plain"),element.getNro()); 
      RequestBody posLatitud = RequestBody.create(MediaType.parse("text/plain"),element.getPosLatitud()); 
      RequestBody posLongitud = RequestBody.create(MediaType.parse("text/plain"),element.getPosLongitud()); 
      RequestBody regimenTenencia = RequestBody.create(MediaType.parse("text/plain"),Integer.toBinaryString(element.getRegimenTenenciaId())); 
      RequestBody regimenOtros = RequestBody.create(MediaType.parse("text/plain"),element.getRegimenOtros()); 
      Call<Establecimiento> establecimientoCall = webDatumApi.sincroEstablecimiento("Token "+prefs.getString("PREF_USER_TOKEN",null),body,nombre,numero,posLatitud,posLongitud,regimenTenencia,regimenOtros); 

      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"); 
       } 
      }); 
関連する問題