java.net.SocketTimeoutException:查询url时读取超时
我得到这个错误:java.net.SocketTimeoutException:查询url时读取超时,java,Java,我得到这个错误: java.net.SocketTimeoutException: Read timed out http://api.trove.nla.gov.au/result?key=<KEY HERE>&zone=all&reclevel=brief&q=%20am%20only%20yours%20fear&encoding=json Exception in thread "main" java.lang.NullPointerExcep
java.net.SocketTimeoutException: Read timed out
http://api.trove.nla.gov.au/result?key=<KEY HERE>&zone=all&reclevel=brief&q=%20am%20only%20yours%20fear&encoding=json
Exception in thread "main" java.lang.NullPointerException at
trove.connect.TroveService.QueryTrove(TroveService.java:134) at
datasetconverter.CsvReader2.main(CsvReader2.java:64) Java Result: 1
BUILD SUCCESSFUL (total time: 1 minute 41 seconds)
java.net.SocketTimeoutException:读取超时
http://api.trove.nla.gov.au/result?key=&zone=all&reclevel=brief&q=%20am%20only%20yours%20fear&encoding=json
线程“main”java.lang.NullPointerException中的异常位于
trove.connect.TroveService.QueryTrove(TroveService.java:134)位于
datasetconverter.CsvReader2.main(CsvReader2.java:64)java结果:1
构建成功(总时间:1分41秒)
当我执行代码时:
public String getJSON(String url, int timeout) throws IOException, Exception {
try {
URL u = new URL(url);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("Content-length", "0");
c.setUseCaches(false);
c.setAllowUserInteraction(false);
c.setConnectTimeout(timeout);
c.setReadTimeout(timeout);
c.connect();
int status = c.getResponseCode();
switch (status) {
case 504:
case 503:
Thread.sleep(10000);
this.QueryTrove(url);
break;
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
return sb.toString();
}
} catch (MalformedURLException ex) {
System.out.println(ex);
System.out.println(url);
} catch (IOException ex) {
System.out.println(ex);
System.out.println(url);
}
return null;
}
public int QueryTrove(String query) throws Exception{
int sum =0;
String json = getJSON("http://api.trove.nla.gov.au/result?key=<KEY HERE>&zone=all&reclevel=brief&q="+query+"&encoding=json",10000);
Gson gson = new Gson();
JsonResponse r = gson.fromJson(json, JsonResponse.class);
for (Zone zone : r.getResponse().getZones()) {
sum = sum + Integer.parseInt(zone.records.getTotal());
}
return sum;
}
publicstringgetjson(stringurl,int-timeout)抛出IOException,Exception{
试一试{
URL u=新的URL(URL);
HttpURLConnection c=(HttpURLConnection)u.openConnection();
c、 setRequestMethod(“GET”);
c、 setRequestProperty(“内容长度”、“0”);
c、 setUseCaches(假);
c、 setAllowUserInteraction(假);
c、 设置连接超时(超时);
c、 setReadTimeout(超时);
c、 connect();
int status=c.getResponseCode();
开关(状态){
案例504:
案例503:
睡眠(10000);
这个.QueryTrove(url);
打破
案例200:
案例201:
BufferedReader br=新的BufferedReader(新的InputStreamReader(c.getInputStream());
StringBuilder sb=新的StringBuilder();
弦线;
而((line=br.readLine())!=null){
sb.追加(第+行“\n”);
}
br.close();
使某人返回字符串();
}
}捕获(格式错误){
系统输出打印项次(ex);
System.out.println(url);
}捕获(IOEX异常){
系统输出打印项次(ex);
System.out.println(url);
}
返回null;
}
public int QueryTrove(字符串查询)引发异常{
整数和=0;
字符串json=getJSON(“http://api.trove.nla.gov.au/result?key=&zone=all&reclevel=brief&q=“+query+”&encoding=json”,10000);
Gson Gson=新的Gson();
JsonResponse r=gson.fromJson(json,JsonResponse.class);
对于(区域:r.getResponse().getZones()){
sum=sum+Integer.parseInt(zone.records.getTotal());
}
回报金额;
}
这是因为
setConnectionTimeout
造成的吗?如何解决这一问题?发生读取超时是因为服务器响应“读取超时”参数的时间长于“读取超时”参数。
但是假设您有一个合理的读取超时值(比如30秒),这并不意味着问题在于您的读取超时太短,可能是服务器卡住了,永远不会返回答案
要进行诊断,您可以执行以下几项操作:
- 首先,尝试查看当您尝试通过浏览器访问时,获得完整响应需要多长时间
- 尝试并消除超时(默认为无超时),然后查看是否收到响应
这也是一个友好的建议,我会将超时作为一个参数传递,只需定义一个合理的超时并直接使用它,除非这是一个非常不寻常的用例,使用这种抽象时没有人希望传递超时。请添加整个堆栈跟踪。没有TroveService.java和CsvReader2.java,很难诊断。它会给你带来Nullpointer异常。CsvReader2调用TroveService,它有这两个函数,异常发生在哪里。你正在使用的
timeout
值是多少?是的,谢谢。您的代码中有一个NPE。TroveService,第134行。有什么可能是空的?请将该类也粘贴到这里。