【问题标题】:CP connection and receiving data using ObjectInputStreamCP连接和使用ObjectInputStream接收数据
【发布时间】:2013-06-17 02:48:35
【问题描述】:

我需要你的帮助。

我正在使用 TCP 连接在 java 服务器和 android 应用程序客户端之间建立 TCP 连接。 假设我将发送一个序列化对象,但是每次在客户端代码都被阻塞在 Obj = (Person)in.readObject;其中 in 是数据对象输入流,Person 是序列化对象。

但是,如果我发送字符串或整数字符串,并且我使用该 Obj = in.readObject;直接

所以我需要知道我可以添加什么才能成功反序列化。

或者可能数组列表只是字符串,所以假设我该怎么做


客户端安卓代码

    lst = (ListView) findViewById(R.id.list);    
         al = new ArrayList<String>();    
         ad = new ArrayAdapter<String>(this,R.layout.list, al);

try { 
      socket = new Socket("192.168.0.103", 8888);
      in = new ObjectInputStream(socket.getInputStream());
      Object obj = null;  
      while ((obj = in.readObject()) != null) {  
          if (obj instanceof Person) {    
    al.add(((Person) obj).toString());

    lst.setAdapter(ad);
    }}      

     } catch (UnknownHostException e) {
      e.printStackTrace();

    } catch (IOException e) {
      e.printStackTrace();

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    finally{
      if (in != null){

    try {
        in.close();

    } catch (IOException e) {
        e.printStackTrace();
       }

Java 服务器代码

try {
   socket = serverSocket.accept();

oos = new ObjectOutputStream(socket.getOutputStream());

Person person = new Person();

person.setFirstName("James");

    person.setLastName("Ryan");

    person.setAge(19);

    String a = "Ahmed";

    oos.writeObject(a);

    oos.writeObject(person);  

   } catch (FileNotFoundException ex) {
       ex.printStackTrace();

   }catch (IOException e) {
    e.printStackTrace();

   }
   finally{

    if( oos!= null){

     try {
        oos.flush(); 
      oos.close();
     } catch (IOException e) {
      e.printStackTrace();
     }

序列化对象

public class Person implements Serializable {

    private String firstName;
    private String lastName;
    private int age; 
    public Person() {
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
       public String toString() {

        StringBuffer buffer = new StringBuffer();
        buffer.append(firstName);
        buffer.append("\n");
        buffer.append(lastName);
        buffer.append("\n");
        buffer.append(age);
        buffer.append("\n");

        return buffer.toString();
    }

【问题讨论】:

  • 发布一些代码。写完脸红了吗?

标签: java android serialization deserialization objectinputstream


【解决方案1】:

这里有几个问题。

首先,readObject() 在 EOS 不返回 null,所以你的循环条件是错误的。它会抛出 EOFException,所以你需要抓住它。

其次,我根本不知道你为什么要使用序列化,如果你想要的只是接收对象上 toString() 的结果。

第三,你的代码中没有任何东西会抛出 FileNotFoundException,所以你为什么要捕获它是一个谜。

第四,如果接收到的对象不是 Person 的实例,那么您的代码将永远不会展示这一点。

【讨论】:

  • 非常感谢您的回复,但您的意思不是 Person please 的实例是什么意思
  • 如果instanceof测试失败,你有一个空块。
猜你喜欢
  • 1970-01-01
  • 2013-07-13
  • 1970-01-01
  • 2013-10-28
  • 2013-02-04
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多