有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java“抓取时超时”URLFetch GAE/J

我使用XMLReader来简单地读取下面这样的提要

URLConnection urlConnection = url.openConnection(); 
XmlReader reader = new XmlReader(urlConnection); 

当这被调用时,我在5秒内收到一个IOException“超时” 所以我试着将超时设置到最大值(10秒),但仍然没有运气,5秒后仍然是一个IOExeption

urlConnection.setConnectTimeout(10000); 

(文件中规定了最大值:http://code.google.com/intl/nl-NL/appengine/docs/java/urlfetch/overview.html

似乎提要的大小太大了。当我调用较小的提要时 它工作正常。有什么解决办法吗?我需要能够调用更大的提要


共 (2) 个答案

  1. # 1 楼答案

    您应该使用setReadTimeout方法设置读取截止日期:

    urlConnection.setReadTimeout(10000); //10 Sec
    

    你应该能够在10秒内下载更大的提要
    如果您仍然有问题,请尝试使用不同的方法

  2. # 2 楼答案

    原因是:

    如果读取超时期间没有可用数据,则会引发异常。从the doc of Oracle

    A SocketTimeoutException can be thrown when reading from the returned input stream if the read timeout expires before data is available for read.

    顺便说一下,ReadTimeoutConnectTimeout不同,读取超时是从主机获取数据的超时,请参见different connection timeout and read timeout

    因此,作为@systempuntoout应答,需要设置读取超时