Jump to content

Serialisation android vers java (pitié aidez moi :D )


alex8276

Recommended Posts

Bonjour à tous,

je rencontre un probleme apparemment de serialisation dans mon code. La serialisation semble se passer correctement mais le serveur (java) est incapable de m'afficher le résultat (invalid type code 00) Je trouve trés peu d'info sur ça, est ce que quelqu'un sait ce que ca veux dire et peux me dire ou mon code est faux (sachant que si je fais un client en java avec le meme code, j'ai pas de probleme). La serialisation est elle identique en java et android ? Voici mon code:

L'asyncTask qui envoie les données

    public class Client extends AsyncTask<Void,Void,String>{

        @[member=override]
        protected String doInBackground(Void... params) {
            try {

                pos.setLatitude(45484);
                pos.setLongitude(654884);
                InetAddress serverAddr = InetAddress.getByName("127.0.0.1");
                String msg = text.getText().toString();
                clientSocket = new Socket(serverAddr, 4444);
                ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
                oos.writeObject(pos);
                //oos.writeChars("toto");
                oos.flush();
                //oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }
        protected void onPostExecute(String result){
            text.setText("message envoyé:"+pos.getLatitude());
        }
    }

La classe serialisé:

public class PositionGps implements Serializable{
    //public static final long serialVersionUID = 0xBADA55;
    private double latitude;
    private double longitude;

    public PositionGps(/*long lat, long longi*/){
        //this.latitude = lat;
        //this.longitude = longi;
    }
    private void writeObject(ObjectOutputStream out){
        try {
            //this.setLongitude(10000);
            //this.setLatitude(25000);
            out.writeDouble(this.getLatitude());
            out.writeDouble(this.getLongitude());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void readObject(ObjectInputStream in){
        try {
            this.setLatitude(in.readDouble());
            this.setLongitude(in.readDouble());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public double getLatitude(){
        return this.latitude;
    }
    public void setLatitude(double latitude){
        this.latitude = latitude;
    }

    public double getLongitude(){
        return longitude;
    }
    public void setLongitude(double longi){
        this.longitude = longi;
    }
}

Et le serveur:

public class Server {

	public static void main(String[] args) throws ClassNotFoundException {
		// TODO Auto-generated method stub
		Socket client = null;
		ServerSocket server= null;
		BufferedReader in;
		PrintWriter out;
		PositionGps pos = new PositionGps();
		ObjectInputStream in1;
		try{
			server = new ServerSocket(4444);
			System.out.println("Server Started...");
			//client = server.accept();
		}catch(Exception e){
			
		}
		try {
			
			while(true){
				client = server.accept();
				
				//in = new BufferedReader(new InputStreamReader(client.getInputStream()));
				in1= new ObjectInputStream(client.getInputStream());
				
				//String distant = in.readLine();
				try{
				pos = (PositionGps)in1.readObject();
				}catch(StreamCorruptedException e){
					System.out.println(e);
				}
				System.out.println("latitude:"+pos.getLatitude());
				System.out.println("longitude: "+pos.getLongitude());
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				client.close();
				server.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}

}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...