开发者

I am getting exception like Attempted read on close stream in following code

开发者 https://www.devze.com 2023-02-18 07:06 出处:网络
IN MainActivity.java DefaultHttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(\"http://192.168.5.10/ijoomer_development/index.php?option=com_ijoomer&plg_name=jomsocial&am

IN MainActivity.java

DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.5.10/ijoomer_development/index.php?option=com_ijoomer&plg_name=jomsocial&pview=user&ptask=login&tmpl=component");
StringBuffer strBuffer = new StringBuffer("<data><username>" + username + "</username><password>" + password + "</password><lat>" + lat +"</lat><long>"+ longi +"</long></data>");
StringEntity strEntity = new StringEntity(strBuffer.toString());
post.setEntity(strEntity);
HttpResponse response = client.execute(post);

InputStream in = response.getEntity().getContent();

MainActivity mActivity = new MainActivity();
System.out.println(mActivity.convertStreamToString(in));

demoHandler = new DemoHandler();
ArrayList<UserBean> users = demoHandler.parse(in);
Log.i("VALUE OF USER",""+users);

IN DemoHandler.java

public ArrayList<UserBean> parse(InputStream in) {
    try {
        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        parser.parse(in, this);

    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return records;
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
    data.replace(0, data.length(), "");

    if(localName.equalsIgnoreCase(ROOT_TAG))
        bean = new UserBean();
}

public void characters(char[] ch, int start, int length) throws SAXException {
    data.append(ch, start, length);
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException
{
    if(localName.equalsIgnoreCase(TAG_USERNAME))
        bean.username = data.toString().trim();
    else if(localName.equalsIgnoreCase(TAG_PASSWORD))
        bean.password = data.toString().trim();
    else if(localName.equalsIgnoreCase(TAG_LAT))
        bean.lat = data.toString().trim();
    else if(localName.equalsIgnoreCase(TAG_LONG))
        records.add(bean);
}

 public String convertStreamToString(InputStream in) throws IOException {
    if (in != null) {
        writer = new StringWriter();

       char[] buffer = new char[1024];
       try {
           Reader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
            } finally {
                in.close();
            }
            return writer.toString();
        } else {       
            return "";
        }
    }
}

I am getting error at line ArrayList<UserBean> users = demoHandler.parse(in); in MainActivity.java

I am getting another error at line parser.parse(in, this); in DemoHandler.java

Exception:

03-19 11:25:03.108: WARN/ExpatReader(297): DTD handlers aren't supported.
03-19 11:25:03.108: WARN/System.err(297): java.io.IOException: Attempted read on closed stream.
03-19 11:25:03.169: WARN/System.err(297):     at org.apache.http.conn.EofSensorInputStream.isReadAllowed(EofSensorInputStream.java:127)
03-19 11:25:03.169: WARN/System.err(297):     at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:176)
03-19 11:25:03.169: WARN/System.err(297):     at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:504)
03-19 11:25:03.169: WARN/System.err(297):     at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:467)
03-19 11:25:03.169: WARN/System.err(297):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:329)
03-19 11:25:03.169: WARN/System.err(297):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:286)
03-19 11:25:03.209: WARN/System.err(297):     at javax.xml.parsers.SAXParser.parse(SAXParser.java:361)
03-19 11:25:03.219: WARN/System.err(297):     at javax.xml.parsers.SAXParser.parse(SAXParser.java:240)
03-19 11:25:03.219: WARN/System.err(297):     at com.demo.parsing.DemoHandler.parse(DemoHandler.java:69)
03-19 11:25:03.219: WARN/System.err(297):     at com.demo.parsing.MainActivity$1.onClick(MainActivity.java:119)
03-19 11:25:03.219: WARN/System.err(297):     at android.view.View.performClick(View.java:2364)
03-19 11:25:03.219: WARN/System.err(297):     at android.view.View.onTouchEvent(View.java:4179)
03-19 11:25:03.219: WARN/System.err(297):     at android.widget.TextView.onTouchEvent(TextView.java:6540)
03-19 11:25:03.219: WARN/System.err(297):     at android.view.View.dispatchTouchEvent(View.java:3709)
03-19 11:25:03.219: WARN/System.err(297):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-19 11:25:03.219: WARN/System.err(297):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-19 11:25:03.219: WARN/System.err(297):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-19 11:25:03.219: WARN/System.err(297):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-19 11:25:03.219: WARN/System.err(297):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
03-19 11:25:03.219: WARN/System.err(297):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
03-19 11:25:03.219: WARN/System.err(297):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
03-19 11:25:03.219: WARN/System.err(297):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
03-19 11:25:03.239: WARN/System.err(297):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
03-19 11:25:03.239: WARN/System.err(297):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 11:25:03.239: WARN/System.err(297):     at android.os.Looper.loop(Looper.java:123)
03-19 11:25:03.239: WARN/System.err(297):     at android.app.ActivityThread.main(ActivityThread.java:4363)
03-19 11:25:03.239: WARN/System.err(297):     at java.lang.reflect.Method.invokeNative(Native Method)
03-19 11:25:03.239: WARN/System.err(297):     at java.lang.reflect.Method.invoke(Method.java:521)
03-19 11:25:03.239: WARN/System.err(297):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-19 11:25:03.239: WARN/System.err(297):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-19 11:25:03.329: WARN/System.err(297):     at dalvik.system.NativeStart开发者_如何学Go.main(Native Method)
03-19 11:25:03.329: INFO/VALUE OF USER(297): []


Well, I see you call this first:

System.out.println(mActivity.convertStreamToString(in));

Assuming that is the same converSTreamToSTring as below in your DemoHandler, this function calls

 in.close();

So you stream is now closed. As the error says, you are trying to do stuff with the stream later on in this line:

ArrayList<UserBean> users = demoHandler.parse(in);

So i'm not surprised you are getting the java.io.IOException: Attempted read on closed stream. error. Because you first Closed the stream, and then tried to do something with it.


I had a similar problem lately and I fixed it by removing method.releaseConnection() at the end of my function. Might help others !

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号