实验目的:
测试pyamf,实现GAE,Flex通信。
实验器材:
pyamf,FLEX3,Python25,IE,GAE SDK
实验准备:
用http://pyamf.org/wiki/GoogleAppEngine教程完成的Flex,GAE代码一份。
见下:
python部分
main.py
Python 代码
1. # -*- coding:utf-8 -*-
2. import wsgiref.handlers
3. import sys
4. from google.appengine.ext import webapp
5. from pyamf.remoting.gateway.google import WebAppGateway
6.
7. class MainPage(webapp.RequestHandler):
8. def get(self):
9. self.response.headers['Content-Type'] = 'text/plain'
10. self.response.out.write('Hello, webapp World!')
11.
12. def echo(data):
13. return ['0','1','2哈哈的的的','哦']#为了测试需要。
14.
15. services = {
16. 'myservice.echo': echo,
17. }
18.
19. def main():
20. application_paths = [('/', WebAppGateway(services)), ('/helloworld', MainPage)]
21. application = webapp.WSGIApplication(application_paths, debug=True)
22. wsgiref.handlers.CGIHandler().run(application)
# -*- coding:utf-8 -*-
import wsgiref.handlers
import sys
from google.appengine.ext import webapp
from pyamf.remoting.gateway.google import WebAppGateway
class MainPage(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, webapp World!')
def echo(data):
return ['0','1','2哈哈的的的','哦']#为了测试需要。
services = {
'myservice.echo': echo,
}
def main():
application_paths = [('/', WebAppGateway(services)), ('/helloworld', MainPage)]
application = webapp.WSGIApplication(application_paths, debug=True)
wsgiref.handlers.CGIHandler().run(application)
flex部分
Mxml 代码
1. <?xml version="1.0" encoding="utf-8"?>
2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">
3. <mx:Script>
4. <![CDATA[
5. import flash.net.*;
6. import mx.controls.Alert;
7. public function init():void{
8. var netConnection:NetConnection = new NetConnection();
9. netConnection.connect("http://localhost:8080/");
10. var responder:Responder = new Responder(onComplete, onFail);
11. netConnection.call("myservice.echo", responder, "Flash talked to PyAMF. They both say hello.");
12. }
13.
14. function onComplete(results):void {
15. //var my:String = String("我就是中文");
16. trace(results);
17. Alert.show(results);
18. /*for each(var thisResult in results){
19. trace(thisResult.toString());
20. }*/
21. }
22.
23. function onFail(results):void {
24. for each (var thisResult in results){
25. trace( thisResult);
26. }
27. }
28. ]]>
29. </mx:Script>
30. </mx:Application>
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">
<mx:Script>
<![CDATA[
import flash.net.*;
import mx.controls.Alert;
public function init():void{
var netConnection:NetConnection = new NetConnection();
netConnection.connect("http://localhost:8080/");
var responder:Responder = new Responder(onComplete, onFail);
netConnection.call("myservice.echo", responder, "Flash talked to PyAMF. They both say hello.");
}
function onComplete(results):void {
//var my:String = String("我就是中文");
trace(results);
Alert.show(results);
/*for each(var thisResult in results){
trace(thisResult.toString());
}*/
}
function onFail(results):void {
for each (var thisResult in results){
trace( thisResult);
}
}
]]>
</mx:Script>
</mx:Application>
实验步骤:
1.打开CMD,用dev_appserver开启GAE服务器
2.在Flex中运行debug命令
实验问题:
修改main.py中echo的返回值后,第一次运行flex不能收到回复,而以后再运行debug就可以收到。
问题初步分析:
开始使用trace,怀疑是trace的问题,后来发现不是。查看服务器记录,发现并没有访问记录,故推断flex没有给GAE发送请求。问题应该在Flex端。但是,具体问题任在调查中。
实验小结:
1.EditPlus害死人啊,默认是ANSI编码格式,让我传中文郁闷了很久。
2.有没有人能告诉我,GAE要传送一个有层次的数据结构用什么啊?XML?有编码器吗?