我正在以这种方式对来自
android的servlet进行两次succssive调用:
//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();
上面的代码运行良好,因为我在关闭输入流后关闭了第一个连接的输出流.但是如果我在发送对象后关闭输出流,则第二个输入流会抛出异常:
java.io.StreamCorruptedException
关闭输入流后,为什么要关闭输出流?
注意
如果有人知道实际答案或正确的原因,为什么它在Android中不起作用,请回答.直到那时我会接受EJP给出的答案 – 这是android中的一个错误.