2011-08-16 19 views
1

现在我正在从web上下载图像。为此,我设置了http连接,如下面的代码。意外的响应代码:403在黑莓中调用图像时

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

我从web.for一个画面是其他图片显示它的错误响应异常代码显示图像successfully.But调用两个图像:403.I我不明白为什么这个问题是occur.How我可以下载来自web的图像。是否需要修改HttpConnection中的任何更改。

请帮帮我。

+3

HTTP 403表示“Forbidden”。你确定你有适当的权限来访问第二个文件吗? –

+0

它访问时,我从浏览器调用。在Android和iPhone的工作 – Ajay

回答

-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; 
     } 
0

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

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

相关问题