意外的响应代码:在Blackberry中调用图像时为403

时间:2011-08-16 07:28:17

标签: blackberry

现在我正在从web上进行图像下载。为此我设置了http连接,如下面的代码。

HttpConnection connection = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
connection.setRequestMethod(HttpConnection.POST);

我正在从web调用两张图片。一张图片显示图片成功。但是对于其他图片显示错误意外的响应代码:403。我不明白为什么会出现这个问题。如何从网上下载图片。 HttpConnection是否需要修改。

请帮帮我。

2 个答案:

答案 0 :(得分:0)

您是在真实手机上测试过的,还是仅仅在模拟器中测试过?

如果您使用的是模拟器,请确保已将其配置为连接到互联网,默认情况下不会将其配置为执行此操作。
BlackBerry emulator not connecting to internet

答案 1 :(得分:-1)

使用此函数,因为我们从http连接获取字节,您需要将这些字节转换为图像,此函数将为您执行此操作,只需在参数中传递图像的URL:

public static Bitmap connectServerForImage(String url) {
    HttpConnection httpConnection = null;
    DataOutputStream httpDataOutput = null;
    InputStream httpInput = null;
    int rc;
    Bitmap bitmp = null;
    try {
        httpConnection = (HttpConnection) Connector.open(url);
        rc = httpConnection.getResponseCode();
        if (rc != HttpConnection.HTTP_OK) {
            throw new IOException("HTTP response code: " + rc);
        }
        httpInput = httpConnection.openInputStream();
        InputStream inp = httpInput;
        byte[] b = IOUtilities.streamToBytes(inp);
        EncodedImage hai = EncodedImage.createEncodedImage(b, 0, b.length);
        int currentWidthFixed32 = Fixed32.toFP(hai.getWidth());
        int currentHeightFixed32 = Fixed32.toFP(hai.getHeight());
        int reqWidth = 48;
        int reqHeight = 35;
        int requiredWidthFixed32 = Fixed32.toFP(reqWidth);
        int requiredHeightFixed32 = Fixed32.toFP(reqHeight);
        int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32);
        int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32);
        hai = hai.scaleImage32(scaleXFixed32, scaleYFixed32);
        return hai.getBitmap();
        } catch (Exception ex) {
            System.out.println("URL Bitmap Error........" +url+ ex.getMessage());
        } finally {
            try {
                if (httpInput != null)
                    httpInput.close();
                if (httpDataOutput != null)
                    httpDataOutput.close();
                if (httpConnection != null)
                    httpConnection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return bitmp;
        }