2013-06-20 149 views
1

如果服務器沒有返回圖像,我想發送imgData爲null。但是使用此代碼它永遠不會返回null.I需要檢查HttpResponse是否包含null或image或者當我從服務器發送響應時出現錯誤消息。如果圖像存在,它將以字節數組的形式發送(如果不存在),則返回null,如果發生錯誤,則發送錯誤消息。檢查HttpResponse中是否有字符串或空值或圖像

public static byte[] getCommentImage(final int commentId) { 
    byte[] imgData = null; 
    HttpResponse res= null; 
    try { 
     res = $.get(COMMENT_IMAGE_URL, new Object[] {"comment_id", commentId }); 
     HttpEntity resEntity = res.getEntity(); 
     imgData = EntityUtils.toByteArray(resEntity); 
     Log.d("getCommentImage API",imgData+" "); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return imgData; 
    } 
+0

你有控制服務器嗎? HttpResponse包含null是什麼意思? – lelloman

+0

如果圖像不存在數據庫我發送數據爲空 – abc

+0

如果你有控制權的服務器,你可以任意決定,響應正文的第一個字節是類型,所以也許0x00爲null,0xaa爲錯誤和0xff的圖像,然後根據類型讀取其他字節 – lelloman

回答

0

HTTP響應通常包含聲明內容類型的頭。 對於圖像「image/jpeg」或「image/png」,「String」(或HTML頁面)通常是內容類型「text/html」。

resEntity.getContentType().getValue(); 

而且你有狀態/結果代碼:

res.getStatusLine().getStatusCode(); 

我認爲這個結果代碼= 200(HTTP_OK)的情況下,你會得到一個空值。

+0

我檢查了這兩種情況resEntity.getContentType()。getValue()是text/html當服務器發送圖像和服務器不發送圖像時 – abc

相關問題