gpt4 book ai didi

java - 输入流和 SAXParser 的问题

转载 作者:行者123 更新时间:2023-11-29 22:07:24 25 4
gpt4 key购买 nike

当我使用 HTTPGET 特别是 TIMEOUT 处理并通过 HttpResponse 和 passto xmlR.parse(new InputSource(instream)) 获取响应时,我在解析时遇到了问题; 并制作休息程序。但有抛出错误:

05-08 11:03:38.637: WARN/System.err(522): java.io.IOException: Attempted read on closed stream.
05-08 11:03:38.667: WARN/System.err(522): at org.apache.http.conn.EofSensorInputStream.isReadAllowed(EofSensorInputStream.java:127)
05-08 11:03:38.667: WARN/System.err(522): at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:176)
05-08 11:03:38.677: WARN/System.err(522): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:515)
05-08 11:03:38.677: WARN/System.err(522): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:478)
05-08 11:03:38.677: WARN/System.err(522): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:317)
05-08 11:03:38.677: WARN/System.err(522): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275)
05-08 11:03:38.677: WARN/System.err(522): at com.housedisplay.mapspec.MyMapActivity.Sendsarchparameter(MyMapActivity.java:334)
05-08 11:03:38.687: WARN/System.err(522): at com.housedisplay.mapspec.MyMapActivity.access$0(MyMapActivity.java:289)
05-08 11:03:38.697: WARN/System.err(522): at com.housedisplay.mapspec.MyMapActivity$SearchATask.doInBackground(MyMapActivity.java:251)
05-08 11:03:38.697: WARN/System.err(522): at com.housedisplay.mapspec.MyMapActivity$SearchATask.doInBackground(MyMapActivity.java:1)
05-08 11:03:38.697: WARN/System.err(522): at android.os.AsyncTask$2.call(AsyncTask.java:252)
05-08 11:03:38.697: WARN/System.err(522): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-08 11:03:38.697: WARN/System.err(522): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-08 11:03:38.697: WARN/System.err(522): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
05-08 11:03:38.697: WARN/System.err(522): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
05-08 11:03:38.697: WARN/System.err(522): at java.lang.Thread.run(Thread.java:1020)

代码::(超时代码基于this answer by kuester2000)

HttpGet httpGet = new HttpGet(strURL.toURI());
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(httpGet);


HttpEntity entity = response.getEntity();
InputStream instream = entity.getContent();
if (entity != null) {

strResponse = convertStreamToString(instream);


}
/**********test*******/
SAXParserFactory saxPF = SAXParserFactory.newInstance();

SAXParser saxP = saxPF.newSAXParser();
XMLReader xmlR = saxP.getXMLReader();
System.out.println("url >>>>>" + strURL);
HandlerFromLatLongCustom myXMLHandler = new HandlerFromLatLongCustom();
xmlR.setContentHandler(myXMLHandler);
xmlR.parse(new InputSource(instream));
instream.close();

最佳答案

我怀疑这是问题所在:

strResponse = convertStreamToString(instream);

我的猜测是该方法(您尚未显示)会关闭流。撇开其他不谈,它至少会读取流,这将使以后很难再次读取...

您可以或者将整个响应读入一个字节数组,然后您可以根据需要将其包装在任意多个 ByteArrayInputStream 中,或者您可以使用 strResponse 作为解析源。不过,这可能会引发编码问题。

HttpClient 可能会为您完成所有这些 - 您可以简单地写:

instream = entity.getContent();

并再次获取相同的数据 - 我对返回的流是否有效“实时”或客户端代码是否读取数据并缓存数据的细节了解不够。

另请注意,这段代码非常可疑:

HttpEntity entity = response.getEntity();
InputStream instream = entity.getContent();
if (entity != null) {
...

如果 entity 为 null,第二行将已经抛出一个 NullPointerException,使得对第三行的检查变得毫无意义......

关于java - 输入流和 SAXParser 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10493214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com