当前位置: 代码迷 >> Android >> android 序列化 class not found,该如何解决
  详细解决方案

android 序列化 class not found,该如何解决

热度:346   发布时间:2016-05-01 21:15:38.0
android 序列化 class not found
最近在做你画我猜。。。。使用socket传递对象,下面的代码如果是java程序的话是没问题的,但是改到android下却有问题了在server 端提示,而且我在服务端也定义了这个Data类的。
java.lang.ClassNotFoundException: com.DrawAndGuess.Data class not found 
其中 com.DrawAndGuess.Data的代码如下
Java code
package com.DrawAndGuess;import java.io.Serializable;public  class Data implements Serializable{    private int x,y;    Data(int x,int y){        this.x = x;        this.y = y;    }    public int getX() {        return x;    }    public void setX(int x) {        this.x = x;    }    public int getY() {        return y;    }    public void setY(int y) {        this.y = y;    }    private static final long serialVersionUID = 1L;    }


Java code
package com.DrawAndGuess;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.net.Socket;import android.app.Activity;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.os.Bundle;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceView;public class DrawAndGuessActivity extends Activity {    DrawAndGuessSurfaceView drawAndGuessSurfaceView;            @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        drawAndGuessSurfaceView = new DrawAndGuessSurfaceView(this);        setContentView(drawAndGuessSurfaceView);                        try{                        Data data = new Data(3,4);            Socket socketConnection = new Socket("172.18.43.59",8889);            ObjectOutputStream clientOutStream = new ObjectOutputStream(socketConnection.getOutputStream());            ObjectInputStream clientInputStream = new ObjectInputStream(socketConnection.getInputStream());                        System.out.println("connect....");            clientOutStream.writeObject(data);                                data = (Data) clientInputStream.readObject();            System.out.println("Receive from server:");            System.out.println("x: "+ data.getX() + "y: " + data.getY());                        clientOutStream.close();            clientInputStream.close();                     }catch (Exception e) {            // TODO: handle exception            e.printStackTrace();        }    }   }


服务器如下
Java code
package testsocket.com;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.net.ServerSocket;import java.net.Socket;public class Server {    public static void main(String[] arg) {        Data data = null;        try{            ServerSocket socketConnection = new ServerSocket(11111);            System.out.println("Server waiting");            Socket  pipe = socketConnection.accept();            ObjectInputStream serverInputStream = new ObjectInputStream(pipe.getInputStream());            ObjectOutputStream serverOutputStream = new ObjectOutputStream(pipe.getOutputStream());            data = (Data) serverInputStream.readObject();            System.out.println("Receive from client:");            System.out.println("x: "+ data.getX() + "y: " + data.getY());            data.setX(5);            data.setY(6);            serverOutputStream.writeObject(data);            serverInputStream.close();            serverOutputStream.close();            System.out.println("server receive");        }catch (Exception e) {            // TODO: handle exception            e.printStackTrace();        }    }        }
  相关解决方案