一个关于 Connection timed out: connect 的问题,求大神指点

吃猫的小金鱼 2013-09-12 02:02:54
我用jdom解析php接口返回的xml,偶尔会报出一下错误,过会自己就好了,这个时间段不确定。错误如下:
ava.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:453)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:891)
at com.mxx.service.MapXML.<init>(MapXML.java:54)
at com.mxx.web.GetjsonAction.getMapJson(GetjsonAction.java:81)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:142)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:166)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.MessageStoreInterceptor.intercept(MessageStoreInterceptor.java:204)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Unknown Source)
...全文
87857 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
TDDing 2015-01-29
  • 打赏
  • 举报
回复
楼主这个问题最后怎么解决的,不知道现在还有什么资料没,现在还有哪位关注这个问题吗
吃猫的小金鱼 2013-09-14
  • 打赏
  • 举报
回复
它是马上报错,没有时间等待的。这符合逻辑吗?
吃猫的小金鱼 2013-09-14
  • 打赏
  • 举报
回复
引用 16 楼 shnulaa 的回复:
看了一下stack class:XMLEntityManager method:setupCurrentEntity 650行左右,如下代码

 URL location = new URL(expandedSystemId); 
 URLConnection connect = location.openConnection();
 if (!(connect instanceof HttpURLConnection)) {
     stream = connect.getInputStream();
 }
以上的代码HttpURLConnection根本就没有设置timeout的数值,基本上是用的默认值。 因为这边没有设置timeout的数值,所以只能使用如下方法 可以通过以下两个语句来设置相应的超时:

System.setProperty("sun.net.client.defaultConnectTimeout", "300000"); 
System.setProperty("sun.net.client.defaultReadTimeout", "300000"); 
其中: sun.net.client.defaultConnectTimeout:连接主机的超时时间(单位:毫秒) sun.net.client.defaultReadTimeout:从主机读取数据的超时时间(单位:毫秒) 试试看吧
我放到这里可以吗,如果没有问题的话,他依然报错
public MapXML(String filePath) {
  /**
   * 申明一个SAXBuilder解析对象
   */
  SAXBuilder builder = new SAXBuilder();
  builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

  System.setProperty("sun.net.client.defaultConnectTimeout", "300000"); 
  System.setProperty("sun.net.client.defaultReadTimeout", "300000"); 

  try {
   
   list = new ArrayList<Photoinfo>();
   
   Photoinfo info = null;
   Document document = builder.build(filePath);
   
   Element eltRoot = (Element)XPath.selectSingleNode(document, "/xml");
   
   List<Element> region = eltRoot.getChildren();
   //System.out.println(listChildNode);
   
   
   for(Element e : region)
   {
	   info = new Photoinfo();
	if(null != e.getChildText("car_id"))
		info.setCnum(e.getChildText("car_id"));
    if(null != e.getChildText("longitude"))
    	info.setLng(e.getChildText("longitude"));
    if(null != e.getChildText("latitude"));
    	info.setLat(e.getChildText("latitude"));
    if(null !=e.getChildText("create_time"))
    	info.setPtime(e.getChildText("create_time"));
    if(null != e.getChildText("photo_url"))
    	info.setPpath(e.getChildText("photo_url"));
    if(null != e.getChildText("province"))
    	info.setAddl1(e.getChildText("province"));
    if(null != e.getChildText("city"))
    	info.setAddl2(e.getChildText("city"));
    if(null != e.getChildText("county"))
    	info.setAddl3(e.getChildText("county"));
    if(null != e.getChildText("village"))
    	info.setAddl4(e.getChildText("village"));
    if(null != e.getChildText("street"))
    	info.setAddl5(e.getChildText("street"));
    if(null != e.getChildText("house_number"))
    	info.setAddl6(e.getChildText("house_number"));
    if(null !=e.getChildText("address"))
    	info.setAddinfo(e.getChildText("address"));
    list.add(info);
   }
   
  } catch (JDOMException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
吃猫的小金鱼 2013-09-14
  • 打赏
  • 举报
回复
现在我让我本地和服务器俩个地方同时在跑这个程序。有时候服务器报这个异常,但本地没事,有时候本地报异常,服务器没事。
晓风吹雾 2013-09-13
  • 打赏
  • 举报
回复
看了一下stack class:XMLEntityManager method:setupCurrentEntity 650行左右,如下代码

 URL location = new URL(expandedSystemId); 
 URLConnection connect = location.openConnection();
 if (!(connect instanceof HttpURLConnection)) {
     stream = connect.getInputStream();
 }
以上的代码HttpURLConnection根本就没有设置timeout的数值,基本上是用的默认值。 因为这边没有设置timeout的数值,所以只能使用如下方法 可以通过以下两个语句来设置相应的超时:

System.setProperty("sun.net.client.defaultConnectTimeout", "300000"); 
System.setProperty("sun.net.client.defaultReadTimeout", "300000"); 
其中: sun.net.client.defaultConnectTimeout:连接主机的超时时间(单位:毫秒) sun.net.client.defaultReadTimeout:从主机读取数据的超时时间(单位:毫秒) 试试看吧
吃猫的小金鱼 2013-09-13
  • 打赏
  • 举报
回复
引用 14 楼 tracy19880727 的回复:
前不久我才做了xml的解析 也是一样的报错 我用的dom4j解析了 报错原因是因为解析xml时要去检验dtd 我加上最开始给你的那句话就好了 你这个原因我觉得跟我那个一样 但是不知道为什么解决不了 期待大神来吧!
我在网上看到关于jom4j的那个问题了,他那个报错和我这个好像有点区别,他报了个关于jom4j的错误,然后才是time out的错误,我这是直接的time out 错误
MC-热狗 2013-09-13
  • 打赏
  • 举报
回复
前不久我才做了xml的解析 也是一样的报错 我用的dom4j解析了 报错原因是因为解析xml时要去检验dtd 我加上最开始给你的那句话就好了 你这个原因我觉得跟我那个一样 但是不知道为什么解决不了 期待大神来吧!
吃猫的小金鱼 2013-09-13
  • 打赏
  • 举报
回复
我ping那个地址,一直能ping通,中间没有间断过
吃猫的小金鱼 2013-09-13
  • 打赏
  • 举报
回复
引用 11 楼 tracy19880727 的回复:
加了我写的那句代码还报错哇? 那就是网络原因了
网络原因偶尔一次还可以,每天都有,每个一两个小时就会报错一两个小时,,,,我实在是找不出来是什么原因。。。。
MC-热狗 2013-09-13
  • 打赏
  • 举报
回复
加了我写的那句代码还报错哇? 那就是网络原因了
吃猫的小金鱼 2013-09-13
  • 打赏
  • 举报
回复
引用 6 楼 xietingyan 的回复:
从堆栈信息看 at org.jdom.input.SAXBuilder.build(SAXBuilder.java:891) at com.mxx.service.MapXML.<init>(MapXML.java:54) at com.mxx.web.GetjsonAction.getMapJson(GetjsonAction.java:81) Document document = builder.build(filePath); 这行代码,这个filePath是不是一个网络路径,是否可以获取到?
filePath确实是个网络路径,报错后,我在浏览器中能获取到xml内容,在程序中会继续报错
吃猫的小金鱼 2013-09-13
  • 打赏
  • 举报
回复
引用 5 楼 tracy19880727 的回复:
每次解析都会报错吗?报错的那行代码都是同一行吗?网络是不是有问题?
不是每次都报错,间接性的,有时候几个小时没问题,有时候几个小时报错。报错都是同一句。是 Document document = builder.build(filePath);
MC-热狗 2013-09-13
  • 打赏
  • 举报
回复
如果你的filePath是一个网络路径 那么网络不畅通会有超时的现象
xiaohuashenshou 2013-09-13
  • 打赏
  • 举报
回复
肯定是网络的问题啦,明显超时了
xietingyan 2013-09-13
  • 打赏
  • 举报
回复
从堆栈信息看 at org.jdom.input.SAXBuilder.build(SAXBuilder.java:891) at com.mxx.service.MapXML.<init>(MapXML.java:54) at com.mxx.web.GetjsonAction.getMapJson(GetjsonAction.java:81) Document document = builder.build(filePath); 这行代码,这个filePath是不是一个网络路径,是否可以获取到?
MC-热狗 2013-09-13
  • 打赏
  • 举报
回复
每次解析都会报错吗?报错的那行代码都是同一行吗?网络是不是有问题?
吃猫的小金鱼 2013-09-13
  • 打赏
  • 举报
回复
按照楼上说的加入了,还是不行
public MapXML(String filePath) {
  /**
   * 申明一个SAXBuilder解析对象
   */
  SAXBuilder builder = new SAXBuilder();
  builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
  try {
   
   list = new ArrayList<Photoinfo>();
   
   Photoinfo info = null;
   Document document = builder.build(filePath);
   
   Element eltRoot = (Element)XPath.selectSingleNode(document, "/xml");
   
   List<Element> region = eltRoot.getChildren();
   //System.out.println(listChildNode);
   
   
   for(Element e : region)
   {
	   info = new Photoinfo();
	if(null != e.getChildText("car_id"))
		info.setCnum(e.getChildText("car_id"));
    if(null != e.getChildText("longitude"))
    	info.setLng(e.getChildText("longitude"));
    if(null != e.getChildText("latitude"));
    	info.setLat(e.getChildText("latitude"));
    if(null !=e.getChildText("create_time"))
    	info.setPtime(e.getChildText("create_time"));
    if(null != e.getChildText("photo_url"))
    	info.setPpath(e.getChildText("photo_url"));
    if(null != e.getChildText("province"))
    	info.setAddl1(e.getChildText("province"));
    if(null != e.getChildText("city"))
    	info.setAddl2(e.getChildText("city"));
    if(null != e.getChildText("county"))
    	info.setAddl3(e.getChildText("county"));
    if(null != e.getChildText("village"))
    	info.setAddl4(e.getChildText("village"));
    if(null != e.getChildText("street"))
    	info.setAddl5(e.getChildText("street"));
    if(null != e.getChildText("house_number"))
    	info.setAddl6(e.getChildText("house_number"));
    if(null !=e.getChildText("address"))
    	info.setAddinfo(e.getChildText("address"));
    list.add(info);
   }
   
  } catch (JDOMException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
吃猫的小金鱼 2013-09-12
  • 打赏
  • 举报
回复
引用 2 楼 tracy19880727 的回复:
SAXBuilder builder = new SAXBuilder(); 在这句话后面加上下面这句话就不会出现这样的错误了 builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 原因是因为解析xml时会联网查找xml里面定义的dtd,这样每次就耗费了大量的时间。 坐等给分!!!
好像不行,问题还是从在
MC-热狗 2013-09-12
  • 打赏
  • 举报
回复
SAXBuilder builder = new SAXBuilder(); 在这句话后面加上下面这句话就不会出现这样的错误了 builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 原因是因为解析xml时会联网查找xml里面定义的dtd,这样每次就耗费了大量的时间。 坐等给分!!!
吃猫的小金鱼 2013-09-12
  • 打赏
  • 举报
回复
这是xml解析方法,其中filePath是接口地址
public class MapXML {
 /**
  * 默认无参构造方法
  */
 public MapXML() {
  
 }
 //申明一个list来保存对象
 public List<Photoinfo> list = null;
 
 /**
  * 构造方法
  * @param xml文件的地址
  */
 public MapXML(String filePath) {
  /**
   * 申明一个SAXBuilder解析对象
   */
  SAXBuilder builder = new SAXBuilder();
  //System.out.println(filePath);
  try {
   
	  System.out.println(filePath);
   list = new ArrayList<Photoinfo>();
   
   Photoinfo info = null;
   
   Document document = builder.build(filePath);
   
   Element eltRoot = (Element)XPath.selectSingleNode(document, "/xml");
   
   List<Element> region = eltRoot.getChildren();
   //System.out.println(listChildNode);
   
   
   for(Element e : region)
   {
	   info = new Photoinfo();
	if(null != e.getChildText("car_id"))
		info.setCnum(e.getChildText("car_id"));
    if(null != e.getChildText("longitude"))
    	info.setLng(e.getChildText("longitude"));
    if(null != e.getChildText("latitude"));
    	info.setLat(e.getChildText("latitude"));
    if(null !=e.getChildText("create_time"))
    	info.setPtime(e.getChildText("create_time"));
    if(null != e.getChildText("photo_url"))
    	info.setPpath(e.getChildText("photo_url"));
    if(null != e.getChildText("province"))
    	info.setAddl1(e.getChildText("province"));
    if(null != e.getChildText("city"))
    	info.setAddl2(e.getChildText("city"));
    if(null != e.getChildText("county"))
    	info.setAddl3(e.getChildText("county"));
    if(null != e.getChildText("village"))
    	info.setAddl4(e.getChildText("village"));
    if(null != e.getChildText("street"))
    	info.setAddl5(e.getChildText("street"));
    if(null != e.getChildText("house_number"))
    	info.setAddl6(e.getChildText("house_number"));
    if(null !=e.getChildText("address"))
    	info.setAddinfo(e.getChildText("address"));
    list.add(info);
   }
   
  } catch (JDOMException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧