gpt4 book ai didi

java - Android 通过UDP套接字发送对象

转载 作者:行者123 更新时间:2023-12-02 00:06:59 27 4
gpt4 key购买 nike

我正在尝试通过 UDP 套接字发送对象 (ClientModel)。套接字部分工作正常,因为如果我尝试发送一个简单的字符串,它就可以工作。我遇到异常,错误在哪里?客户说“客户:失败。”。这是代码,目前只有客户端,因为问题在于发送过程,而不是我可以考虑接收这些东西:)

谢谢

    public class ClientActivity extends Activity {
public static final int SERVERPORT = 5050;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.client);

try {
InetAddress serverAddr = getBroadcastAddress();

DatagramSocket socket = new DatagramSocket();

//byte[] buf = "whatsup bitches".getBytes();

ClientModel sendingMessage = new ClientModel();
sendingMessage.data = 999;
sendingMessage.name = "sample";
System.out.println(sendingMessage);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(sendingMessage);
byte[] buf = baos.toByteArray();


DatagramPacket packet = new DatagramPacket(buf, buf.length,
serverAddr, SERVERPORT);
socket.send(packet);
Log.d("UDP", "Client: Packet sent to server on address: " + serverAddr);
}
catch (Exception e) {
Log.d("UDP", "Client: Fail");
}
}

private InetAddress getBroadcastAddress() throws IOException {
WifiManager myWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
DhcpInfo myDhcpInfo = myWifiManager.getDhcpInfo();
if (myDhcpInfo == null) {
System.out.println("Could not get broadcast address");
return null;
}
int broadcast = (myDhcpInfo.ipAddress & myDhcpInfo.netmask)
| ~myDhcpInfo.netmask;
byte[] quads = new byte[4];
for (int k = 0; k < 4; k++)
quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
return InetAddress.getByAddress(quads);
}

class ClientModel {
public int data;
public String name;
}
}

这是日志,抱歉:

11-29 17:10:09.933: I/System.out(5700): com.example.ClientActivity$ClientModel@4054b800
11-29 17:10:09.964: W/System.err(5700): java.io.NotSerializableException: com.example.ClientActivity$ClientModel
11-29 17:10:09.964: W/System.err(5700): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1535)
11-29 17:10:09.964: W/System.err(5700): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
11-29 17:10:09.964: W/System.err(5700): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
11-29 17:10:09.964: W/System.err(5700): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
11-29 17:10:09.964: W/System.err(5700): at com.example.ClientActivity.onCreate(ClientActivity.java:39)
11-29 17:10:09.964: W/System.err(5700): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-29 17:10:09.964: W/System.err(5700): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
11-29 17:10:09.964: W/System.err(5700): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
11-29 17:10:09.964: W/System.err(5700): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
11-29 17:10:09.964: W/System.err(5700): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
11-29 17:10:09.964: W/System.err(5700): at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 17:10:09.964: W/System.err(5700): at android.os.Looper.loop(Looper.java:130)
11-29 17:10:09.964: W/System.err(5700): at android.app.ActivityThread.main(ActivityThread.java:3835)
11-29 17:10:09.964: W/System.err(5700): at java.lang.reflect.Method.invokeNative(Native Method)
11-29 17:10:09.964: W/System.err(5700): at java.lang.reflect.Method.invoke(Method.java:507)
11-29 17:10:09.964: W/System.err(5700): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
11-29 17:10:09.964: W/System.err(5700): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
11-29 17:10:09.964: W/System.err(5700): at dalvik.system.NativeStart.main(Native Method)
11-29 17:10:09.964: D/UDP(5700): Client: Fail

最佳答案

您需要了解有关 java 序列化的更多信息。

class ClientModel implements Serializable{
public int data;
public String name;
}

关于java - Android 通过UDP套接字发送对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13629577/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com