如果服務器沒有返回圖像,我想發送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;
}
你有控制服務器嗎? HttpResponse包含null是什麼意思? – lelloman
如果圖像不存在數據庫我發送數據爲空 – abc
如果你有控制權的服務器,你可以任意決定,響應正文的第一個字節是類型,所以也許0x00爲null,0xaa爲錯誤和0xff的圖像,然後根據類型讀取其他字節 – lelloman