Android Socket将Object转换为ArrayList

时间:2013-01-07 11:25:02

标签: java android sockets object

我有从Android到Java应用程序的客户端服务器连接。我正在做的是使用writeObject编写一个ArrayList,然后我可以在执行readObject后将其强制转换回ArrayList但是我得到了这个异常:

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)

Android应用程序告诉服务器获取具有命令GETUSERS的用户

这是在MainActivity.java

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

这是在抛出异常的ConnectionToServer.java部分(转换它的行):

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;
}

这就是服务器将其发送到Android应用程序(客户端)的方式:

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]");}
            }

这就是所谓的方法:

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;
    }

感谢您的帮助,我非常感谢:)

1 个答案:

答案 0 :(得分:2)

查询中缺少字段thumb_url。因此resultSet.getString("thumb_url"))失败,服务器发送字符串"[ERROR]",客户端无法将其反序列化为List。

您可能会执行以下操作:

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

但我认为在服务器端捕获异常会更好