views:

67

answers:

0

2 times out of 10, i will get a 404 response code when i run the code below. can someone please give me some hint why i even get a 404? if i'm missing a command that could prevent a 404 from happening, please point that out. the end goal is to get an image from a remote server and place it in a Bitmap object. nothing complicated. also, i get the same results if i use the ".GET" request method or not. When i was working with BB JRE 4.6, i was using the ConnectionDescriptor and ConnectionFactory objects and i NEVER got a 404. but since i have to develop my code for JRE 4.3, i can't use them since obviously they don't exist in 4.3. btw, MDS is running and i perform these 10 tests one after another. Many thanks. Cheers!!!

    HttpConnection httpConnection = null;
    Bitmap bmpImageRetrieved = null;
    DataInputStream dis = null;

    try
    {
        httpConnection = (HttpConnection) Connector.open(uri, Connector.READ);
        httpConnection.setRequestProperty("Accept","image/jpg, image/jpeg, image/png, image/gif");
        //httpConnection.setRequestMethod(HttpConnection.GET);

        int responseCode = httpConnection.getResponseCode();

        if (responseCode == HttpConnection.HTTP_OK)
        {
            int intResponseLength = (int) httpConnection.getLength();

            if (intResponseLength > 0)
            {
                byte[] bData = new byte[intResponseLength];
                dis = httpConnection.openDataInputStream();
                dis.readFully(bData);
                bmpImageRetrieved = Bitmap.createBitmapFromBytes(bData, 0, intResponseLength, 1);
            }
            else
            {
                throw new Exception("The length of the response is less than zero.");
            }
        }
        else
        {
            throw new Exception("Response code is not good. ResponseCode: "+ responseCode);
        }
    }
    catch(Exception exc)
    {
        throw new Exception("Unexpected error received: "+ exc.toString());
    }
    finally
    {
        if (dis != null)
        {
            dis.close();
            dis = null;
        }

        if (httpConnection != null)
        {
            httpConnection.close();
            httpConnection = null;
        }
    }

    return (bmpImageRetrieved == null ? Bitmap.getBitmapResource("nopic.jpg") : bmpImageRetrieved);