2016-09-14 44 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