开发者

Java - Casting an Object to a ByteArrayEntity (Android) ClassCastException

I have a ByteArrayEntity as follows:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
tempPic.compress(CompressFormat.PNG, 0, bos);
byte[] bitmapdata = bos.toByteArray();
photoByteArray = new ByteArrayEntity(bitmapdata);

tempPic is of type android.graphics.Bitmap.

What I then have to do is make a request using an AsyncTask that takes an array of Objects which I later then cast to their various types. However, when attempting to cast my ByteArrayEntity later on, I get a ClassCastException, I was wondering if anyone could expla开发者_高级运维in this?

protected HttpResponse doInBackground(Object... httpRequest) 
{
    ByteArrayEntity dataPhoto = null;

    // Further code


    if(myCondition)
    {
        dataPhoto = (ByteArrayEntity)httpRequest[2];
    }

}

I really need to get this working, but don't really have time currently to reimplement this completely, so any hacks or workarounds would be appreciated. I'm working with Android 2.2

The whole aim is to take an image from the Android camera, then setEntity of my HttpRequest to a ByteArrayEntity and POST my image to a server where this is then handled.


Rather than passing an Object[] and then casting based on the position in the array, why not just pass a bean?

public class MyBean {
    private ByteArrayEntity myByteArrayEntity;
    private String someString;

    // getters and setters
}

No casting required and much easier to maintain/extend.


You might use a different type parameterization of AsyncTask so that you don't need to cast. If you're using an anonymous class then:

new ArrayTask<ByteArrayEntity, Void, HttpResponse>() {
    ...

So that you can do:

@Override
protected HttpResponse doInBackground(ByteArrayEntity... byteArrays) {
    ...
    dataPhoto = byteArrays[2];
    ...

if your arguments are indeed intended to be of type ByteArrayEntity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜