2016-09-14 103 views
0

一點點的鹹菜在這裏。我正在閱讀Zip文件中的JSON,並且想要使用JSON的內容填充Vaadin中的表格。爪哇 - Vaadin - 表沒有填充

這裏是我的函數來讀取的東西,並填寫表,這是Java。

private void getJsonContent() { 
     try { 
      FileInputStream fis = new FileInputStream(backupFile); 
      ZipInputStream zin = new ZipInputStream(new BufferedInputStream(fis)); 
      ZipEntry entry; 
      byte[] buffer = new byte[1024]; 
      while((entry = zin.getNextEntry()) != null) { 
       if(entry.getName().equalsIgnoreCase("content.json")) { 
        int n; 
        while((n = zin.read(buffer, 0, 1024)) > -1){ 
         String JSON = new String(buffer, Charset.forName("UTF-8")); 
         JSONObject obj = new JSONObject(JSON); 

         logger.info(JSON); 

         // Assign "global" Values to Variables 
         this.createdAt = obj.getString("created_at"); 
         this.version = obj.getString("version"); 

         // Fill table if applicable      
         for(int i = 0; i < obj.getJSONArray("content").length(); i++) { 
          JSONObject sub = obj.getJSONArray("content").getJSONObject(i); 
          logger.info(sub); 

          infoTable.addItem(new Object[] { 
            sub.get("imported_identities").toString(), 
            sub.get("project_versions").toString(), 
            sub.get("last_import").toString(), 
            sub.get("client").toString(), 
            sub.get("project").toString() 
          }, i +1); 
         } 
        } 
       } 
      } 
      zin.close(); 
      fis.close(); 
     } catch (FileNotFoundException e) { 
      // Can't happen here 
     } catch (IOException e) { 
      logger.info("Can't read File."); 
     } catch (JSONException jse) { 
      logger.info("JSON Content could not be read: " + jse.getMessage()); 
     } 
    } 

你會發現我有一個函數調用logger.info(sub) - 以確保我所得到的是另一種有效的JSON對象(我讀的文件包含嵌套的東西)

輸出:

{"imported_identities":0,"project_versions":0,"last_import":null,"client":"Client1","project":"Project2"} 
{"imported_identities":0,"project_versions":0,"last_import":null,"client":"Client2","project":"Project1"} 
{"imported_identities":0,"project_versions":1,"last_import":"2016-09-14T09:28:24.520Z","client":"Client1","project":"Project1"} 

我確保所有的值都是正確的(並且表格默認爲null) - 這是表格屬性:

infoTable.addContainerProperty(impIds, String.class, null); 
    infoTable.addContainerProperty(projVe, String.class, null); 
    infoTable.addContainerProperty(lstImp, String.class, null); 
    infoTable.addContainerProperty(client, String.class, null); 
    infoTable.addContainerProperty(projct, String.class, null); 

    infoTable.setColumnCollapsingAllowed(true); 
    infoTable.setColumnCollapsed(impIds, true); 
    infoTable.setColumnCollapsed(projVe, true); 
    infoTable.setColumnCollapsed(lstImp, true); 

最後,該表對其調用了「refreshRowCache」。任何人都看到了問題?有沒有錯誤的,什麼都沒有,表只是不添加的項目(的infoTable.getItemIds().size()大小0後調用右邊是

編輯:

我嘗試以下,以驗證

infoTable.addItem(i + 1); 
infoTable.getItem(i + 1).getItemProperty(impIds).setValue(sub.get("imported_identities").toString()); 
infoTable.getItem(i + 1).getItemProperty(projVe).setValue(sub.get("project_versions").toString()); 

這又引起一個NullPointerException異常,堆棧跟蹤但不包含任何我的課的,據我可以看到

+0

作爲第一步,我將設置一個POJO類,併爲每個單元格連成員。使用包含JSON數據的POJO成功填充Java容器後,可以使用容器(例如BeanItemContainer)將其綁定到表。 'table.setContainerDataSource(new BeanItemContainer <>(POJO.class,yourListOfPojos))' –

+0

Nevermind。過了太久。在我嘗試填充表格之前,我的表格未正確初始化,導致出現可預期的空白點。其餘的代碼工作。 – Eskir

回答

2

以下是錯誤的:

  1. String構造函數需要讀取大小(n)。

      while ((n = zin.read(buffer, 0, 1024)) > -1) { 
           String JSON = new String(buffer, 0, n, StandardCharsets.UTF_8); 
    
  2. 然後你在1024的位置做最多1024 JSONs在循環中,而不是在它的所有

  3. 一個UTF-8字節不能在某個時刻被拆分說一個JSON和期望在結束和接下來的塊開始時具有有效的完整多字節序列。

  4. 也有readFullycloseEntry失蹤。

簡而言之:

private void getJsonContent() { 
    try (ZipInputStream zin = new ZipInputStream(new BufferedInputStream(
      new FileInputStream(backupFile)))) { 
     ZipEntry entry; 
     while ((entry = zin.getNextEntry()) != null) { 
      if (entry.getName().equalsIgnoreCase("content.json")) { 
       long size = entry.getSize(); 
       if (size > 100_000) { 
        throw new IllegalArgumentException("Data too large"); 
       } 
       // We could use an InputStreamReader and read text piecewise. 
       // However JSON parsing also is easiest on an entire text. 
       byte[] buffer = new byte[(int)size]; 
       int n = zin.readFully(buffer, 0, buffer.length); 
       String json = new String(buffer, StandardCharsets.UTF_8); 
       JSONObject obj = new JSONObject(json); 
       logger.info(json); 

       // Assign "global" Values to Variables 
       this.createdAt = obj.getString("created_at"); 
       this.version = obj.getString("version"); 

       // Fill table if applicable      
       for (int i = 0; i < obj.getJSONArray("content").length(); i++) { 
        JSONObject sub = obj.getJSONArray("content").getJSONObject(i); 
        logger.info(sub); 

        infoTable.addItem(new Object[] { 
          sub.get("imported_identities").toString(), 
          sub.get("project_versions").toString(), 
          sub.get("last_import").toString(), 
          sub.get("client").toString(), 
          sub.get("project").toString() 
        }, i + 1); 
       } 
      } // if 
      zin.closeEntry(); // Do not forget preparing for the next entry 
     } 
    } catch (IOException e) { 
     logger.info("Can't read File."); 
    } catch (JSONException jse) { 
     logger.info("JSON Content could not be read: " + jse.getMessage()); 
    } 
} 

試戴與資源甚至異常,或者返回自動關閉。

+0

看看上面的評論。仍然感謝給我一些改進我的代碼的想法,在我回來檢查之前,我遇到了你描述的問題。(不完整/損壞的字節序列) – Eskir

+0

事情我注意到,方法「readFully」不可見,我堅持read()那裏 – Eskir