Java 如何获得大响应体Jsoup

Java 如何获得大响应体Jsoup,java,android,jsoup,Java,Android,Jsoup,告诉我问题是什么,以及如何解决。我需要得到一个json格式的响应数组。在服务器上,响应大约在30分钟内形成,收集150K个元素的数组。然后是调度,并等待超过24小时。但即使过了这么长时间,答案还是没有出现。服务器上没有错误。如果你选择10公里,那么答案是没有问题的。我试图将答案保存到一个TXT文件中,它的大小是+-35MB,包含35K个库存 这是我执行请求的方式: Connection.Response response = Jsoup.connect(URLs.sayt + "/&q

告诉我问题是什么,以及如何解决。我需要得到一个json格式的响应数组。在服务器上,响应大约在30分钟内形成,收集150K个元素的数组。然后是调度,并等待超过24小时。但即使过了这么长时间,答案还是没有出现。服务器上没有错误。如果你选择10公里,那么答案是没有问题的。我试图将答案保存到一个TXT文件中,它的大小是+-35MB,包含35K个库存

这是我执行请求的方式:

Connection.Response response = Jsoup.connect(URLs.sayt + "/" + URLs.api + "/hs/CLIENTS/LIST/")
                    .method(Connection.Method.GET)
                    .maxBodySize(0)
                    .timeout(0)
                    .userAgent(URLs.getUserAgentMobile())
                    .header("Authorization", "Basic " + base64login)
                    .header("Accept-Language", "*")
                    .header("mode", "tobase")
                    .execute();
            if(response != null){
                ArrayList<Client> clients = new Gson().fromJson(response.body(), new TypeToken<List<Client>>() {}.getType());
                for (Client client:clients){
                    sqLiteWork.AddToClient(client);
                }
            }

我还没有找到解决我问题的办法。我尝试了okhttp和httpurlconnection。显然,android客户端本身不支持主体中如此大的字符串响应。也许这是一个通过文件的解决方案。但当您需要实际数据时,这不是一个解决方案。因此,我从服务器对10K元素做出了响应。满载150K元件需要2小时

请注意,我说的是最新数据,如果2小时很长,那么我的应用程序也支持在线模式

Connection connection = Jsoup.connect(URLs.sayt + "/" + URLs.api + "/hs/CLIENTS/LIST/")
                    .method(Connection.Method.GET)
                    .ignoreContentType(true)
                    .maxBodySize(0)
                    .timeout(0)
                    .userAgent(URLs.getUserAgentMobile())
                    .header("Authorization", "Basic " + base64login)
                    .header("Accept-Language", "*")
                    .header("mode", "tobase");

            Connection.Response response = connection.execute();
            ArrayList<Client> clients = new Gson().fromJson(response.body(), new TypeToken<List<Client>>() {}.getType());
            Client last = null;
            for (Client client:clients){
                last = client;
                sqLiteWork.AddToClient(client);
            }

            while (clients.size() > 0){
                response = connection.header("last", last.id).execute();
                clients = new Gson().fromJson(response.body(), new TypeToken<List<Client>>() {}.getType());
                for (Client client:clients){
                    last = client;
                    sqLiteWork.AddToClient(client);
                }
            }
Connection Connection=Jsoup.connect(url.sayt+“/”+url.api+“/hs/CLIENTS/LIST/”)
.method(Connection.method.GET)
.ignoreContentType(true)
.maxBodySize(0)
.超时(0)
.userAgent(URL.getUserAgentMobile())
.header(“授权”、“基本”+base64登录)
.header(“接受语言”和“*”)
.header(“mode”、“tobase”);
Connection.Response=Connection.execute();
ArrayList clients=new Gson().fromJson(response.body(),new TypeToken(){}.getType());
Client last=null;
用于(客户端:客户端){
last=客户;
sqLiteWork.AddToClient(客户端);
}
while(clients.size()>0){
response=connection.header(“last”,last.id).execute();
clients=new Gson().fromJson(response.body(),new TypeToken(){}.getType());
用于(客户端:客户端){
last=客户;
sqLiteWork.AddToClient(客户端);
}
}

为什么要使用jsoup作为http客户端?在我看来,或者说是这样,我问的是如何获取和可能存在的问题,而不是使用什么更好。我不太了解您不能做什么。您遇到的问题是什么?这个问题看起来好像你的HTTP服务器java程序需要24小时来计算答案。您的HTTP服务器是否需要24小时来计算客户端请求的答案?是这样吗?如果需要很长时间,也许您应该预先计算答案,以便在客户机发出请求时下载。您是否拥有并控制HTTP服务器中的软件?如果你这样做了,让客户等24小时等待回复听起来像是一个错误,你必须首先改变。可能我说得很糟糕。我写了服务器端,它工作正常。反应在30分钟内形成。并发送给客户。但是客户端等待或处理响应超过24小时,仍然没有收到响应。我试图通过浏览器打开它,一切正常。我尝试将数组减少到10k元素,但仍然有效。但是对于一个150k项的大数组,客户端会无休止地等待或处理响应。代码的最后一部分是为
ArrayList客户端中的每个元素建立回服务器的连接。这部分对我来说毫无意义。代码中的最后一个while循环是为
clients数组列表中的每个元素建立一个连接。您已经清楚地声明,此
ArrayList
中包含10000或150000个元素。这意味着您将建立150000个回服务器的连接。我很惊讶这个代码没有花你两个星期!看来你没有仔细阅读我的代码。循环中有一行:
response=connection.header(“last”,last.id).execute()
和每个请求后的
last.id
是不同的。因此,连接只进行了15次。仔细看看代码。是的,我有。今天早上我看了很长时间。一次又一次。坦率地说——你需要重新评估你的30分钟或24小时将走向何方。您所做的事情涉及到数据库更新,这里没有人可以帮助您,因为DB方法没有发布,而且您还有其他对服务器的查询。当这些循环运行时,为什么不努力打印出时间戳,这样您就可以准确地确定是什么花费了这么长的时间。我的意思是,显然下载一组数据不应该,但反复执行相同的查询应该是正确的。打印出
System.currentTimeMillis()
并用我测得的数据重新发布你的问题,没有问题。从请求到数据库中的完整记录,每10K项需要10分钟。还请注意,客户端数组总是在更改
clients=new Gson().fromJson(response.body(),new TypeToken(){}.getType())在循环中。当它的长度等于零时,循环就会中断。我上面的新代码按预期工作。不过,我还有一个问题,我还没有找到答案。android中的字符串响应是否仍有限制?
Connection connection = Jsoup.connect(URLs.sayt + "/" + URLs.api + "/hs/CLIENTS/LIST/")
                    .method(Connection.Method.GET)
                    .ignoreContentType(true)
                    .maxBodySize(0)
                    .timeout(0)
                    .userAgent(URLs.getUserAgentMobile())
                    .header("Authorization", "Basic " + base64login)
                    .header("Accept-Language", "*")
                    .header("mode", "tobase");

            Connection.Response response = connection.execute();
            ArrayList<Client> clients = new Gson().fromJson(response.body(), new TypeToken<List<Client>>() {}.getType());
            Client last = null;
            for (Client client:clients){
                last = client;
                sqLiteWork.AddToClient(client);
            }

            while (clients.size() > 0){
                response = connection.header("last", last.id).execute();
                clients = new Gson().fromJson(response.body(), new TypeToken<List<Client>>() {}.getType());
                for (Client client:clients){
                    last = client;
                    sqLiteWork.AddToClient(client);
                }
            }