简体   繁体   English

Android Socket将Object转换为ArrayList

[英]Android Socket convert Object to ArrayList

I have a Client Server Connection from Android to a Java Application. 我有从Android到Java应用程序的客户端服务器连接。 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: 我正在做的是使用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)

The Android Application tells the Server to get the Users with the Command GETUSERS Android应用程序告诉服务器使用命令GETUSERS获取用户

This is in MainActivity.java 这是在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): 这是在抛出异常的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;
}

And this is how the Server sends it to the Android Application (Client): 这就是服务器将其发送到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]");}
            }

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 :) 谢谢你的帮助我真的很感激:)

The field thumb_url is missing from the query. 查询中缺少字段thumb_url So resultSet.getString("thumb_url")) fails and the server send the string "[ERROR]" , which cannot be deserialized as a List by the client. 因此resultSet.getString("thumb_url"))失败,服务器发送字符串"[ERROR]" ,客户端无法将其反序列化为List。

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 但我认为最好不要在服务器端捕获异常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM