为什么要在的OutputStream机器人的InputStream后关闭 [英] Why should OutputStream be closed after inputstream in android

查看:296
本文介绍了为什么要在的OutputStream机器人的InputStream后关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从机器人这样做两succssive调用我的servlet:

  //第一个连接
网址URL =新的URL(http://172.16.32.160:8080/xyz/check_availability);
            HttpURLConnection的连接=(HttpURLConnection类)url.openConnection();
            connection.setDoOutput(真);
            ObjectOutputStream的出=新的ObjectOutputStream(connection.getOutputStream());            字符串=XYA;
            字符串B =XSW;
            out.writeObject(一);            了out.flush();
            在ObjectInputStream的=新的ObjectInputStream(connection.getInputStream());
            字符串s =(字符串)
            in.readObject();
            附寄();
            out.close();                Toast.makeText(getApplicationContext(),1,Toast.LENGTH_LONG).show();
            //第二个连接
            网址为url1 =新的URL(http://172.16.32.160:8080/xyz/check_availability);
            HttpURLConnection的连接1 =(HttpURLConnection类)url1.openConnection();
            connection1.setDoOutput(真);
            ObjectOutputStream的OUT1 =新的ObjectOutputStream(connection1.getOutputStream());
            out1.writeObject(二);
            out1.flush();            ObjectInputStream的IN1 =新的ObjectInputStream(connection1.getInputStream());
                字符串str =(字符串)
                in1.readObject();
                in1.close();
                out1.close();
                Toast.makeText(getApplicationContext(),2,Toast.LENGTH_LONG).show();

以上code效果很好,因为我已经关闭的InputStream后关闭第一个连接的输出流。但如果我在发送对象后,要关闭的OutputStream,第二个输入流抛出异常:

  java.io.StreamCorruptedException

为什么要关闭的OutputStream InputStream的后关闭?_爱
注意结果
    如果有人知道实际的答案或正当理由,为什么它不工作的Andr​​oid,    请回答。在那之前,我会接受EJP给出了答案 - 这是在android系统中的错误。


解决方案

看起来像在Android中的错误给我。

在Java的何处这一来,关闭的ObjectOutputStream 随时通过一个 HttpURLConnection类不做任何事,除了强制输出(因为连接具有熬夜接收响应)。关闭的输入流 HttpURLConnection类关闭整个连接,这样的的ObjectOutputStream 会做什么。<随后关闭/ p>

我怀疑Android的做坏事的连接时,你做的 ObjectOutputStream.close()第一,如关闭它。

我就省略了 ObjectOutputStream.close()干脆,你不需要它在任何平台上。在刷新()就足够了。

I am doing two succssive calls to my servlet from android in this way:

//FIRST CONNECTION
URL url = new URL("http://172.16.32.160:8080/xyz/check_availability");
            HttpURLConnection connection =(HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            ObjectOutputStream out=new ObjectOutputStream(connection.getOutputStream());

            String a="xya";
            String b="xsw";
            out.writeObject(a);

            out.flush();


            ObjectInputStream in=new ObjectInputStream(connection.getInputStream());
            String s=(String)
            in.readObject();
            in.close();
            out.close();

                Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_LONG).show();


            //SECOND CONNECTION 


            URL url1 = new URL("http://172.16.32.160:8080/xyz/check_availability");
            HttpURLConnection connection1 = (HttpURLConnection)url1.openConnection();
            connection1.setDoOutput(true);


            ObjectOutputStream out1=new ObjectOutputStream(connection1.getOutputStream());
            out1.writeObject(b);
            out1.flush();

            ObjectInputStream in1=new ObjectInputStream(connection1.getInputStream());
                String str=(String)
                in1.readObject();
                in1.close();
                out1.close();
                Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_LONG).show();

The above code works well because I've closed the outputstream of the first connection after closing the inputstream. But If I close the outputstream after sending the object, the second input stream throws an exception:

java.io.StreamCorruptedException

Why should the outputstream be closed after closing the inputstream?

NOTE
    If someone knows the actual answer or the proper reason as to why it does not work in Android,     please answer. Till then I will accept the answer given by EJP - that it is a bug in android.

解决方案

Looks like a bug in Android to me.

In Java whence this comes, closing the ObjectOutputStream at any time over an HttpURLConnection does nothing except force the output (because the connection has to stay up to receive the response). Closing the input stream of an HttpURLConnection closes the entire connection, so a subsequent close of the ObjectOutputStream would do nothing.

I suspect Android does something bad to the connection when you do the ObjectOutputStream.close() first, such as closing it.

I would omit the ObjectOutputStream.close() altogether, you don't need it in either platform. The flush() is sufficient.

这篇关于为什么要在的OutputStream机器人的InputStream后关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆