Mitor
Mitor

Reputation: 83

java.lang.ClassCastException: java.lang.Object[] cannot be cast to

I am trying to send an Object through Sockets using AsyncTask, but i'm getting this error (i just post the important part of it):

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
                      Process: com.mitorapps.beberconmemes, PID: 6418
                      java.lang.RuntimeException: An error occurred while executing doInBackground()



   Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.mitorapps.beberconmemes.data.Persona[]
at com.mitorapps.beberconmemes.conexion.Enviar_Persona$MyATaskCliente.doInBackground(Enviar_Persona.java:47)

This is my Persona Class(The object that i want to send):

 package com.mitorapps.beberconmemes.data;

    import java.io.Serializable;

    public class Persona implements Serializable{
        private String Name_Persona;
        String Mac_Address;

        public Persona(String name_persona, String mac_address) {
            this.Name_Persona = name_persona;
            this.Mac_Address = mac_address;
        }   

        public String getMac_Address() {
            return this.Mac_Address;
        }

        public String getName_Persona() {
            return this.Name_Persona;
        }

        public void setName_Persona(String name_Persona) {
            this.Name_Persona = name_Persona;
        }
    }

This is my Client Class (named as Enviar_persona):

 package com.mitorapps.beberconmemes.conexion;    

    public class Enviar_Persona extends Cliente{
        private AsyncTask myATaskYW;
        public Enviar_Persona(Context context){
            super(context);//the client class forces to receive a context
            SERVERPORT=5000;
        }
        @Override
        public void enviar_persona(Persona p){//Method Called from view
            this.myATaskYW = new MyATaskCliente();
            this.myATaskYW.execute(p);
        }
        public void update_view(String s){//To update the State of an element on UI
            TextView resp = (TextView) ((Activity)c).findViewById(R.id.resp);
            resp.setText(s);

        }
        //AsyncTask class
        class MyATaskCliente extends AsyncTask<Persona, Void, String> {

            ProgressDialog progressDialog;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                System.out.println("OnPreExecute");
                progressDialog = new ProgressDialog(c);
                progressDialog.setCanceledOnTouchOutside(false);
                progressDialog.setTitle("Connecting to server");
                progressDialog.setMessage("Please wait...");
                progressDialog.show();
            }
            @Override
            protected String doInBackground(Persona... persona) {
                try {
                    //here i'm using an static ip
                    Socket socket = new Socket("192.168.1.7", SERVERPORT);
                    //Object Send to server
                    ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
                    output.writeObject(persona[0]);
                    //Response recivied from server
                    InputStream stream = socket.getInputStream();    
                    byte[] len=new byte[256];
                    stream.read(len,0,256);
                    String received = new String(len,"UTF-8");
                    socket.close();
                    return received;

                } catch (UnknownHostException ex) {
                    return ex.getMessage();
                } catch (IOException ex) {
                    return ex.getMessage();
                }

            }

            @Override
            protected void onPostExecute(String value) {
                progressDialog.dismiss();
                update_view(value);
            }
        }
    }

I did it using the AsyncTask On the main activity and it works perfectly, but now that I need group my code in different classes i got the error

Upvotes: 2

Views: 1257

Answers (1)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

Instead of using

AsyncTask myATaskYW;

use

MyATaskCliente myATaskYW;

because AsyncTask is generic so the signature is replaced with Object[] array but you need to work with the specific implementation.

Note : Person[] is not a child of Object[]

Upvotes: 2

Related Questions