0

I have a Client Server Connection from Android to a Java Application. What I am doing is writing a ArrayList with writeObject so I can then cast it back to a ArrayList after doing readObject but I get this Exception:

01-07 11:10:08.821: E/AndroidRuntime(1314): Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.ArrayList
01-07 11:10:08.821: E/AndroidRuntime(1314):     at com.williamhenry.audiobolle.ConnectionToServer.getUsers(ConnectionToServer.java:127)

The Android Application tells the Server to get the Users with the Command GETUSERS

This is in MainActivity.java

adapter=new ListAdapter(this, new ConnectionToServer().execute("GETUSERS").get());

This is in the ConnectionToServer.java part where the Exception is thrown (The line where it is converted):

private ArrayList<HashMap<String, String>> getUsers()
{
ArrayList<HashMap<String, String>> usersList = new ArrayList<HashMap<String, String>>();

try {
    Socket socket = new Socket(url, 8001);
    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
    oos.writeObject("GETUSERS");

    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

    usersList = (ArrayList<HashMap<String, String>>) ois.readObject();

    ois.close();
    oos.close();


} catch (UnknownHostException e) {} catch (IOException e) {} catch (ClassNotFoundException e) {}
return usersList;
}

And this is how the Server sends it to the Android Application (Client):

else if(messageArray[0].equals("GETUSERS"))
            {
                try {
                    //st.setUserFullName(messageArray[1], messageArray[2], messageArray[3]);
                    oos.writeObject(st.getUsers());
                } catch (SQLException e) {oos.writeObject("[ERROR]");}
            }

And this is the Method that is called:

public ArrayList<HashMap<String, String>> getUsers() throws SQLException
    {
        ArrayList<HashMap<String, String>> usersList = new ArrayList<HashMap<String, String>>();
        Statement statement = conn.createStatement();
        try {

            String query = "SELECT userID,username,status,fullname,lastonline FROM users";
            ResultSet resultSet = statement.executeQuery(query);
            try { 
                while (resultSet.next()) {
                    HashMap<String, String> map = new HashMap<String, String>();

                    // In Hashmap das Key und den Wert reinschreiben
                    map.put(KEY_ID, resultSet.getString("userID"));
                    map.put(KEY_USERNAME, resultSet.getString("username"));
                    map.put(KEY_STATUS, resultSet.getString("status"));
                    map.put(KEY_LASTONLINE, resultSet.getString("lastonline"));
                    map.put(KEY_THUMB_URL, resultSet.getString("thumb_url"));

                    // Hashlist in die ArrayList einfügen
                    usersList.add(map);
                }
            } finally {
                resultSet.close();
            }
        } finally {
            statement.close();
        }
        return usersList;
    }

Thank you for your help I really appreciate it :)

1
  • so you receive string using socket and want to convert it to arraylist? Commented Jan 7, 2013 at 15:31

1 Answer 1

1

The field thumb_url is missing from the query. So resultSet.getString("thumb_url")) fails and the server send the string "[ERROR]", which cannot be deserialized as a List by the client.

You probably can do something like :

Object result = ois.readObject();
if ("[ERROR]".equals(result)) {
   // something was wrong server side
   throw SomeException();
} else {
   usersList = (ArrayList<HashMap<String, String>>) result;
}

but I think it would be even better not to catch the Exception on server side

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.