2013-03-17 133 views
2

我想從php讀取數據,並使用JSON對象獲取數據。我想從數據庫中獲得的其中一個數據是BLOB。我自己試着用下面的代碼。從Android的JSON對象獲取BLOB

products = new JSONArray(getJSONUrl(url)); 
// looping through product 
for (int i = 0; i < products.length(); i++) { 
    JSONObject jsonObj = products.getJSONObject(i); 
    try { 
     placeName = jsonObj.getString(String.valueOf(TAG_PlaceName)); 
     placeDesc = jsonObj.getString(TAG_PlaceDesc); 
     Blob blob = (Blob) jsonObj.get(TAG_placeIcon); 
     byte[] byteBlob = blob.getBytes(0, (int) blob.length()); 
     Bitmap bmp = BitmapFactory.decodeByteArray(byteBlob, 0, byteBlob.length); 
     rowItems.add(new RowItem(placeName, placeDesc, bmp)); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
} 
  • RowItem是我保持數據的類。

任何人都可以指出我的代碼有什麼問題。我試圖環顧幾個小時,但仍然找不到解決方案。

順便說一下,當我嘗試運行應用程序,看來UI沒有任何問題。只有我用來顯示數據的列表視圖看起來沒有任何內容。

+0

它拋出異常? – 2013-03-17 15:17:39

+0

根本沒有錯誤 – TheTree 2013-03-17 16:44:31

回答

0

使用此代碼

 @Override 
     protected Bitmap doInBackground(String... params) { 
      String id = params[0]; 
      String add = "http://user localhost/ImageUpload/getImage.php?id="+id; 
      URL url = null; 
      Bitmap image = null; 
      try { 
       url = new URL(add); 
       image = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return image; 
     }