android中的inputstream
我在活动A中使用了两个输出流和两个输入流。OnclickListener中为按钮“b1”使用了一组输入和输出流。onclickListener中的另一个设置用于按钮“b2”。当我在ClickListener上使用按钮“b2”内的输入流时,我得到了android中的inputstream,android,inputstream,Android,Inputstream,我在活动A中使用了两个输出流和两个输入流。OnclickListener中为按钮“b1”使用了一组输入和输出流。onclickListener中的另一个设置用于按钮“b2”。当我在ClickListener上使用按钮“b2”内的输入流时,我得到了java.io.StreamCorruptedException。当我关闭按钮“b1”中的inputStream时会发生这种情况 但当我不关闭按钮“b1”中的inputstream时,即使我每次创建新的inputstream对象,它在b2中也可以正常工作
java.io.StreamCorruptedException
。当我关闭按钮“b1”中的inputStream时会发生这种情况但当我不关闭按钮“b1”中的inputstream时,即使我每次创建新的inputstream对象,它在b2中也可以正常工作。这是什么原因?
这是我使用的代码
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try
{
URL url = new URL("http://172.16.32.160:8080/xyz/abc");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
ObjectOutputStream out=new ObjectOutputStream(connection.getOutputStream());
String s="r1"+","+receive_name;
Toast.makeText(getApplicationContext(), "sent", Toast.LENGTH_LONG).show();
out.writeObject(s);
out.flush();
out.close();
ObjectInputStream in = new ObjectInputStream(connection.getInputStream());
String output=(String)in.readObject();
}
});
//Code for b2 onclick Listener
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
String receive_name="abcd";
long value=6127261;
String pass=Long.toString(value);
String send="r2"+","+receive_name+","+pass;
URL url = new URL("http://172.16.32.160:8080/xyz/abc");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
ObjectOutputStream out=new ObjectOutputStream(connection.getOutputStream());
out.writeObject(send);
out.flush();
out.close();
//**This Input Stream gives java.io.StreamCorruptedException **
//when the Inputstream of previous(b1's) OnClickListener is closed
//Works when previous Input Stream is not closed
ObjectInputStream in = new ObjectInputStream(connection.getInputStream());
Toast.makeText(getApplicationContext(), "one", Toast.LENGTH_LONG).show();
String output=(String)in.readObject();
}
});
请查看代码中的注释。请发布相关代码。@ThinkStip:我已经输入了代码。见编辑后的问题