在客户端中通过套接字将对象转换为数组

时间:2014-05-19 15:16:53

标签: java android sockets

我正在为在Java服务器中连接的游戏构建Android客户端。我在服务器中有这个代码

for (int i = 0; i < diceRolled.length; i++) {
            diceRolled[i] = (int) ( 1 + Math.random() * 6);
}
List<int[]> dice = Arrays.asList(diceRolled);
output.writeObject(dice);

创建一个Object,它将它通过套接字传递给客户端。 但在客户端我需要将其转换回数组。我试过了

try {
        ObjectInputStream what = new ObjectInputStream(socket.getInputStream());
        int[] myDice = (int[]) what.readObject();
    } catch (StreamCorruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

但它没有用。我的错误在哪里?

编辑: LOGCAT日志

05-19 15:15:18.030: E/AndroidRuntime(1155): FATAL EXCEPTION: main
05-19 15:15:18.030: E/AndroidRuntime(1155): android.os.NetworkOnMainThreadException
05-19 15:15:18.030: E/AndroidRuntime(1155):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at libcore.io.IoBridge.recvfrom(IoBridge.java:503)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at libcore.io.Streams.readFully(Streams.java:81)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at java.io.DataInputStream.readShort(DataInputStream.java:169)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:2098)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at java.io.ObjectInputStream.<init>(ObjectInputStream.java:372)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at com.android.pickominoclient.GameActivity$1.onClick(GameActivity.java:65)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at android.view.View.performClick(View.java:3511)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at android.view.View$PerformClick.run(View.java:14105)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at android.os.Handler.handleCallback(Handler.java:605)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at android.os.Looper.loop(Looper.java:137)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at java.lang.reflect.Method.invokeNative(Native Method)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at java.lang.reflect.Method.invoke(Method.java:511)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-19 15:15:18.030: E/AndroidRuntime(1155):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

服务器发送 List<int[]> dice = Arrays.asList(diceRolled);

申请尝试“重建”int[] myDice = (int[]) what.readObject();

你应该转换为不列出数组