逐步从页面中读取JSON

时间:2016-01-26 14:53:41

标签: java json api rest httprequest

我有一个从Web服务读取JSON的java客户端。 thejson响应的第一部分(偏移1 - 20)具有以下格式。我现在可以将数据的第一页解析成文件(尽管我最终会喜欢解析为db)。如何让客户端解析下一页(偏移21-40)?为简洁起见,文件的第二部分有意省略了更多信息。我使用了apache http客户端,但似乎没有办法处理这个问题?如果指向正确的指示,将不胜感激。

SELECT XMLElement("PropertySet",
                    XMLATTRIBUTES(
                    'TEST' AS "OrderId", 
                    '' AS "DHLCode"
                    )
                 )
FROM DUAL;

1 个答案:

答案 0 :(得分:0)

非常感谢所有答案和指向我的问题。下面的代码执行我想要的操作,即抓取json文件并逐步保存在磁盘上。我认为它仍然是非常基础的,因此我会感谢对更好的方法,编码风格的所有批评。还在学习!!

        while (count < totalCount ) {
                MyClient check = new MyClient();
                check.checkSecurity();

                System.out.println(" ************ Starting with count " + count + " ***********************");
                URI uriBuilder = new URIBuilder(url)
               .addParameter("limit", String.valueOf(limit))
               .addParameter("offset", String.valueOf(offset))
                .build();   

                System.out.println("The URL built is --> " + uriBuilder); 
                HttpGet hget = new HttpGet(uriBuilder);
                System.out.println(hget.getRequestLine());

        CloseableHttpClient httpclient = HttpClients.createDefault();
                hget.addHeader("Content-Type", "application/json");             

                HttpResponse response = httpclient.execute(hget);                                       
                    URL vdbUrl = uriBuilder.toURL();
                    System.out.println("Response status : " + response.getStatusLine());
                    HttpEntity entity2 = response.getEntity();

                    do {
                        vdbInput = new File("C:/myfiles/entry_0" + filenamePostfix + ".json");
                        FileUtils.copyURLToFile(vdbUrl, vdbInput);        
                        System.out.println("File successfully written from page " +  count );
                        System.out.println("page is : "  + vdbUrl);
                        filenamePostfix++;                                                  
                        count++;
                        offset = offset + 20;

                    } while (filenamePostfix < count) ;

                    EntityUtils.consume(entity2);
                }
        }