2010-07-09 75 views
0

我的應用程序循環了大約200個url,都是jpg圖片。 在模擬器中,它讀取正常,然後將字節數組存儲在persistentStore中,沒有任何問題。 在設備上,它給出了java.io.IOException:TCP讀取基本上在每個圖像上超時。 每時每刻,一個人都可以通過。甚至不知道如何。圖像尺寸也不能提供洞察力。有些是6k,有些是11k。大小似乎並不重要,因爲超時。

我會嘗試發佈我認爲是相關的代碼,但我並不是真正的專家,所以如果我遺漏了某些東西,請說出來。通過循環

呼叫HTTP連接和連接螺紋:黑莓Http連接超時圖像下載。爲什麼?

for(int i = 0; i < images.size(); i ++) 
    { 
     try { 
     String url = images.elementAt(i).toString(); 
       HttpRequest data3 = new HttpRequest(url, "GET", false); 
      data3.start(); 

    data3.join(); 

    } catch (IOException e) { 
    Dialog.inform("wtf " + e); 
    } 
    } 

請在HttpConnection的類與適當的後綴實際的連接:

try 
    { 
    HttpConnection connection = (HttpConnection)Connector.open(url + updateConnectionSuffix()); 


    int responseCode = connection.getResponseCode(); 
    if(responseCode != HttpConnection.HTTP_OK) 
    { 
    connection.close(); 
    return; 
    } 

    String contentType = connection.getHeaderField("Content-type"); 
    long length = connection.getLength(); 

    InputStream responseData = connection.openInputStream(); 
    connection.close(); 

    outputFinal(responseData, contentType, length); 
    } 
    catch(IOException ex) 
    { 

    } catch (SAXException ex) { 

    } catch (ParserConfigurationException ex) { 

    } 

最後,讀取流和寫入字節到字節數組:

else if(contentType.equals("image/png") || contentType.equals("image/jpeg") || contentType.equals("image/gif")) 
    { 
    try 
      { 
    if((int) length < 1) 
    length = 15000; 

      byte[] responseData = new byte[(int) length]; 
       int offset = 0; 
       int numRead = 0; 
       StringBuffer rawResponse = new StringBuffer(); 

       int chunk = responseData.length-offset; 
       if(chunk < 1) 
       chunk = 1024; 

       while (offset < length && (numRead=result.read(responseData, offset, chunk)) >= 0){ 
       rawResponse.append(new String(responseData, offset, numRead)); 
       offset += numRead; 
       } 

       String resultString = rawResponse.toString(); 
       byte[] dataArray = resultString.getBytes(); 

       result.close(); 

       database db = new database(); 
       db.storeImage(venue_id, dataArray); 
      } 
      catch(Exception e) 
      { 
      System.out.println(">>>>>>>----------------> total image fail: " + e); 
      } 


    } 

需要考慮的事情:
模擬器中的長度始終爲字節長度。在設備中它總是-1。
塊var是一個測試,看看我是否強制一個15k字節的數組,它會試圖按預期讀取,因爲byte [-1]給出了一個越界異常。結果是一樣的。有時它寫道。大多數情況下它會超時。

任何幫助表示讚賞。

回答

2

您可以使用參數'ConnectionTimeout'來調整Blackberry上TCP超時的長度。

在你的代碼在這裏:

HttpConnection connection = (HttpConnection)Connector.open(url + updateConnectionSuffix()); 

你要追加ConnectionTimeout。你可以把它寫入updateConnectionSuffix()或者只是附加它。

HttpConnection connection = (HttpConnection)Connector.open(url + updateConnectionSuffix() + ";ConnectionTimeout=54321"); 

這將超時設置爲54321毫秒。

超時發生在客戶端正在等待服務器發送ack並且在指定時間內沒有獲得ack時發生。

編輯:另外,你能夠使用瀏覽器和東西?您可能還想玩設備參數。

1

我認爲這個問題可能是您在從輸入流中讀取字節之前關閉連接。嘗試在讀入字節後移動connection.close()。

+0

謝謝!我應用了這兩種解決方案,應用程序正在按預期下載和存儲圖像。如果我有時間進一步調試,我可以精確地指出哪個解決了這個問題,並將最佳解決方案的功勞歸功於未來的程序員可以從中準確獲益。 – Kai 2010-07-12 15:34:55

+0

+1發現,這是很多代碼,我沒有閱讀; D – 2010-07-13 00:19:20