2016-12-29 4 views
1

umgewandelt werden Ich verwende Retrofit2, um zu versuchen, ein Bild an den Server zu senden. Ich verwende das Befehlsmuster dazu. Ich bin den folgenden Fehler erhalten:Retrofit2: ClassCastException: java.util.ArrayList kann nicht in Klasse

com.gary.test.api.commands.AddMediaCommand$1 cannot be cast to java.util.List

So habe ich Klasse AddMediaCommand

public class AddMediaCommand implements Commander { 
    private final TypedByteArray media; 
    private static final int QUALITY = 100; 
    private final Context context; 

    public AddMediaCommand(Context ctx, Bitmap image) { 
     this.context = ctx; 

     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     image.compress(Bitmap.CompressFormat.JPEG, QUALITY, out); 

     media = new TypedByteArray("image/jpeg", out.toByteArray()){ 
      @Override 
      public String fileName() { 
       return "file.jpg"; 
      } 
     }; 
    } 

    @Override 
    public void execute() { 
     new AddMediaService(context, new CommanderListener() { 
      @Override 
      public void onResultReceived(Bundle extras) { 
       sendBroadcastResult(extras); 
      } 
     }).addMedia(media); 
    } 

    private void sendBroadcastResult(Bundle extras) { 
     Intent intent = new Intent(BroadcastActions.BROADCAST_ADD_MEDIA); 
     intent.putExtras(extras); 
     LocalBroadcastManager.getInstance(context).sendBroadcast(intent); 
    } 
} 

Dies führt AddMediaService

public class AddMediaService extends BaseService { 

    private final Context context; 
    private final String server; 
    private static final String TAG = "AddMediaService"; 

    public AddMediaService(Context context, CommanderListener listener) { 
     this.context = context; 
     this.server = ApiPreferences.getInstance(context).getDapiDaftServerApi(); 
     setCommanderListener(listener); 
    } 

    public void addMedia(TypedByteArray image) { 
     Call<MediaModel> mdMediaModelCall = getRetrofit2DapiService(context).addMedia(image); 

     mdMediaModelCall.enqueue(new retrofit2.Callback<MediaModel>() { 
      @Override 
      public void onResponse(Call<MediaModel> call, retrofit2.Response<MediaModel> response) { 
       handleSuccess(response.body()); 
       Log.d(TAG, "Success"); 
      } 

      @Override 
      public void onFailure(Call<MDMediaModel> call, Throwable t) { 
       Log.d(TAG, "Failure"); 
      } 
     }); 
    } 

    protected void handleSuccess(MediaModel model) { 
     Bundle bundle = new Bundle(2); 
     bundle.putInt(Extras.RESPONSE_CODE, ResponseCodes.OK); 
     bundle.putParcelable(Extras.PARAM_MEDIA, model); 
     sendResult(bundle); 
    } 
} 

Die addMedia Retrofit2 Methode aufgerufen ist als

@retrofit2.http.Multipart 
@retrofit2.http.POST("/media") 
Call<MediaModel> addMedia(@retrofit2.http.Part("file") TypedByteArray image); 
folgt

Ich bin dabei, von Retrofit 1.9 auf Retrofit 2 umzusteigen. Das funktionierte in 1.9 ohne Probleme, also weiß ich nicht genau, was dieses Problem ist. Ich fand this, die etwas ähnliches war. Aber dieses OP wirft seinen Code ein und ich bin es nicht.

Wenn mir jemand helfen kann, würde ich es sehr zu schätzen wissen. Ich habe viele meiner API-Anrufe aktualisiert und es gab dieses Problem nicht. Jede Hilfe würde sehr geschätzt werden.

EDIT - MEDIAMODEL CLASS

public class MediaModel implements MediaImage { 

    //Used to mark a error in the media transfer. 
    public static final int NETWORK_ERROR_MEDIA_ID = -100; 

    Integer id; 
    String url; 
    int order; 
    private String message; 
    private Thumbnails thumbnails; 
    private transient WeakReference<Bitmap> temporaryImage; 

    public MediaModel() { 
    } 

    public String getMessage() { 
     return message; 
    } 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getUrl() { 
     return url; 
    } 

    public void setUrl(String url) { 
     this.url = url; 
    } 

    public void setOrder(int order) { 
     this.order = order; 
    } 

    public Thumbnails getThumbnails() { 
     return thumbnails; 
    } 

    public void setTemporaryImage(Bitmap image) { 
     if (image == null) { 
      if (temporaryImage != null) { 
       temporaryImage.clear(); 
      } 
     } else { 
      this.temporaryImage = new WeakReference<>(image); 
     } 
    } 

    public Bitmap getTemporaryImage() { 
     if (temporaryImage == null) { 
      return null; 
     } else { 
      return temporaryImage.get(); 
     } 
    } 
    @Override 
    public void setImageUrl(String url) { 
     setUrl(url); 
    } 

    @Override 
    public String getImageUrl(int imageType) { 
     if (imageType == THUMBNAIL_IMAGE_TYPE){ 
      return getThumbnails().getUrl(); 
     } 
     return getUrl(); 
    } 

    public static class Thumbnails { 
     private String large; 

     public Thumbnails(String largeUrl) { 
      this.large = largeUrl; 
     } 

     public String getUrl() { 
      return large; 
     } 

     public void setUrl(String url) { 
      this.large = url; 
     } 
    } 

    public static final Creator<MediaModel> CREATOR = new Creator<MediaModel>() { 
     public MediaModel createFromParcel(Parcel source) { 
      return new MediaModel(source); 
     } 

     public MediaModel[] newArray(int size) { 
      return new MediaModel[size]; 
     } 
    }; 

    private MediaModel(Parcel in) { 
     id = (Integer) in.readValue(Integer.class.getClassLoader()); 
     url = in.readString(); 
     order = in.readInt(); 
     message = in.readString(); 
     thumbnails = new Thumbnails(in.readString()); 
    } 

    @Override 
    public int describeContents() { 
     return 0; 
    } 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeValue(id); 
     dest.writeString(url); 
     dest.writeInt(order); 
     dest.writeString(message); 
     dest.writeString(thumbnails == null ? EMPTY_STRING : thumbnails.getUrl()); 
    } 
} 
+0

können Sie Code von 'MediaModel' Klasse teilen finden können? –

+0

@RahulSonpaliya Beitrag bearbeitet, um Klasse einzuschließen –

Antwort

0

In Ihrem AddMediaCommand erstellen ResquestBody Objekt als solches:

File file = new File("file.jpg"); 
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); 

dann in Ihrem addMedia Methode innerhalb AddMediaService die MultipartBody erstellen Objekt mit:

MultipartBody.Part.createFormData("file", file.getName(), requestFile)

schließlich die Retrofit Webservice-Schnittstelle ändern, um ein MultipartBody.Part Objekt zu verwenden:

@retrofit2.http.Multipart 
@retrofit2.http.POST("/media") 
Call<MediaModel> addMedia(@retrofit2.http.Part("file") MultipartBody.Part image); 

Sie hier weitere Informationen https://github.com/square/retrofit/issues/1063

Verwandte Themen