1

当我使用HTTPGET特殊的 TIMEOUT 处理并获得响应槽HttpResponse和 passtoxmlR.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)

代码::

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();
4

1 回答 1

2

我怀疑这是问题所在:

strResponse = convertStreamToString(instream);

我的猜测是那个方法(你没有显示)关闭了流。除此之外,它至少会读取流,这将使其以后难以再次读取......

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

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

instream = entity.getContent();

并再次获得相同的数据 - 我对返回的流是否有效“实时”或客户端代码是否读取数据并缓存它的详细信息知之甚少。

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

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

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

于 2012-05-08T05:57:52.970 回答