2013-01-07 68 views
0

我有一個從Android到Java應用程序的客戶端服務器連接。我做的是寫有的writeObject一個ArrayList中,所以我可以那麼做的readObject後,將它轉換回一個ArrayList的,但我得到這個異常:Android Socket將對象轉換爲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; 
    } 

謝謝你的幫助我真的很感激:)

+0

所以你收到串在服務器端的異常,並希望將其轉換到arraylist? – hardartcore

回答

2

查詢中缺少該字段thumb_url。因此resultSet.getString("thumb_url"))失敗,並且服務器發送字符串"[ERROR]",該字符串不能被客戶端反序列化爲列表。

你或許可以這樣做:

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

,但我認爲這將是更好的使用插座趕上

+0

是的,謝謝你,這是解決方案。 – alexj