Java Serializable Object未被接收或发送到客户端Android应用程序

时间:2015-08-09 12:18:50

标签: java android serialization

My Java Server运行一个线程,只创建一个带有四个参数的Order对象。然后使用writeObject()方法调用通过ObjectOutputStream发送此Object(Order实现Serializable)。我不确定手头的问题,因为客户端没有读取/接收对象。我用String对象测试了这个函数,它运行正常。我相信它可能需要对我的Order类实现做些什么。我已提供以下代码供审核:

订单类

public class Order implements java.io.Serializable{
    private static final long serialVersionUID = 345634l;
    String cuisine;
    String description;
    int min = 0;
    int max = 0;

    public Order(String cuis,String descr,int min,int max){
        cuisine = cuis;
        description = descr;
        this.min = min;
        this.max = max;
    }

    public void setCuisine(String c){
        cuisine = c;
    }

    public String getCuisine(){
        return cuisine;
    }

    public void setDescription(String d){
        description = d;
    }

    public String getDescription(){
        return description;
    }

    public void setMinPrice(int c){
        min = c;
    }

    public int getMin(){
        return min;
    }

    public void setMac(int x){
        max = x;
    }

    public int getMax(){
        return max;
    }

    @Override
    public String toString(){
        return cuisine + "\n" + description + "\n" + min + "\n" + max + "\n" + "$";
    }
}

ListSender线程(服务器端)

class ListSender extends Thread {

    @Override
    public void run() {
        while (true) {                
            try {                 
                ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
                Order r = new Order("Indian","Spicy Madras",4,9);
                orderList.add(r);
                oos.writeObject(r);
                oos.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

MessageHandler线程(Android客户端)

class MessageHandler extends AsyncTask<Void,Void,Void> {
            int orderHead = 0;
            String item = "";

            public Void doInBackground(Void... param){
                try{
                    serverAddr = InetAddress.getByName(ipAddress);
                    socket = new Socket(serverAddr,portNumber);
                    ObjectInputStream ois = new  ObjectInputStream(socket.getInputStream());
                    Order e = (Order) ois.readObject();
                    onPostExecute(e.toString());
                } catch(ClassNotFoundException e) {
                    e.printStackTrace();
                } catch(IOException e){
                    e.printStackTrace();
                }
                return null;
            }

        public void onPostExecute(String s){
            foodList += s + "\n";
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    try{
                        message.setText(foodList);
                    }catch(Exception e){

                    }
                }
            });
        }
    }

我希望这些信息足以让任何人分析并解释此代码的替代或改进版本。

1 个答案:

答案 0 :(得分:-1)

找到解决方案:

确保序列化中使用的所有远程类也应具有相同的包名称。