我有一台接受JSONObject的服务器和一个将JSONObject发送到服务器的Android应用程序。问题是服务器不喜欢它接收的JSONObject的格式。谁能告诉我我做错了什么?
Android客户端:
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)