我有一个接受 JSONObject 的服务器和一个将 JSONObject 发送到服务器的 Android 应用程序。问题是服务器不喜欢它接收的 JSONObject 的格式。谁能告诉我我做错了什么?
安卓客户端:
try{
Socket s = new Socket("67.194.98.244",13375);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
org.json.JSONObject json = new org.json.JSONObject();
json.put("order", id);
oos.writeObject(json);
oos.flush();
oos.close();
}catch(Exception e){
e.printStackTrace();
}
和python服务器:
class MyTCPServerHandler(SocketServer.BaseRequestHandler):
def handle(self):
try:
data = json.loads(self.request.recv(1024).strip())
# process the data, i.e. print it:
for key, value in data.iteritems():
with print_lock:
print key, value
if key == 'order':
SubmitOrder(value,self)
elif key == 'status':
getStatus(value,self)
else:
with print_lock:
print "no order"
except Exception, e:
print "Exception while receiving message: ", e
这是 Android 上的堆栈跟踪:
09-21 21:53:49.804: W/System.err(27827): java.io.StreamCorruptedException
09-21 21:53:49.804: W/System.err(27827): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1528)
09-21 21:53:49.804: W/System.err(27827): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
09-21 21:53:49.804: W/System.err(27827): at com.austinn.hamster.Print.sendCommand(Print.java:173)
09-21 21:53:49.804: W/System.err(27827): at com.austinn.hamster.Print$1$1.run(Print.java:92)
09-21 21:53:52.144: W/System.err(27827): java.io.StreamCorruptedException
09-21 21:53:52.144: W/System.err(27827): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1528)
09-21 21:53:52.154: W/System.err(27827): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
09-21 21:53:52.154: W/System.err(27827): at com.austinn.hamster.Print.sendCommand(Print.java:173)
09-21 21:53:52.154: W/System.err(27827): at com.austinn.hamster.Print$1$1.run(Print.java:92)