2011-08-16 20 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

相關問題