所谓grails写的api即为如下的风格:
http://localhost:8080/ZheLe/API/latestZheItem 返回JSON格式
http://localhost:8080/ZheLe/API/latestZheItem?type=1 返回JSON格式
http://localhost:8080/ZheLe/API/latestZheItem?type=2 返回XML格式
通过render as XML 或者 render as JSON。这样做的好处是可以让flex的httpservice访问到并解析xml,甚至让手机客户端也能调用。
刚开始时,
url包含&符合,httpservice会报错,于是重新设计api,改用其他符号,这样就不报错了。
这也许不是最佳方案,但是目前可行,如果有更好的办法,以后会考虑替换。以下一段代码是范例:
/**
* 用户登陆的API,需要输入的两个参数是userName、password
* 输入userName、password的情况例如:http://localhost:8080/ZheLe/API/login/test?password=test 返回JSON格式
* 输入userName、password、type的情况例如:http://localhost:8080/ZheLe/API/login/test?password=test/1 返回JSON格式
* 输入userName、password、type的情况例如:http://localhost:8080/ZheLe/API/login/test?password=test/2 返回XML格式
*/
def login = {
def userName = params.id
def str = params.password
def strs = str.split("/")
def password = null
def type = null
if (strs.length == 2) {
password = strs[0]
type = strs[1]
} else {
password = strs[0]
}
def user = User.findWhere(userName:userName,password:password)
if (user) {
if("1".equals(type) || type == null || "".equals(type)) {
render user as JSON
}
else if ("2".equals(type)) {
render (contentType:"text/xml",encoding:"UTF-8") {
list {
UserName(user.userName)
Password(user.password)
Email(user.email)
City(user.city)
Attentions(user.attentions)
}
}
}
else
render(text:"<xml><user>type参数没有设置正确</user></xml>",contentType:"text/xml",encoding:"UTF-8")
}
else
render(text:"<xml><user>not found user</user></xml>",contentType:"text/xml",encoding:"UTF-8")
}
分享到:
相关推荐
FLEX _HTTPService的调用方法 显示数据的例子 QueryDataSet.xml
一个flex 与 java 通过HTTPService 通信的小例子,希望对flex新手有点帮助
1、HttpService.dll是一个用于http/https协议访问网络获取数据的链接库,该链接库封装了C#的HttpWebRequest用于请求数据的Get和Post的操作方式,直接输入一个网址即可实现获取对应网页的源码,为网络编程中必不可少...
httpservice post get传数据
简单的 flex HttpService例子,可供初学者参考
flex4 httpservice 例子 提取xml数据
Flex HTTPService 如何给后台传递参数 包含实例绝对简单
.net 下用asp.net实现的HttpService例子; HttpService的部署方式跟网站的部署方式一样; 运行HttpClient前注意先配置下配置文件,Url为HttpService的地址
flex 和java的通信使用的是httpservice 但当时遇到了不知道如何获取java端的返回值给flex前端 我这个就是为了解决你烦恼而来的 绝对不忽悠你
Flex4_httpService远程调用JAVA。包含源码。jar包,配置。
ionic-angular封装网络请求, httpService.ts,封装post,get,基于angular HttpClient
flex通过httpService读取xml数据到datagird中,通过读取远程URL的XML数据(这里保存到本地),保存到ArrayCollection中,然后在将ArrayCollection绑定到datagird上 环境:flex3.0 可直接运行
NULL 博文链接:https://gedoua.iteye.com/blog/679265
Flex中利用HTTPService MXML标签(tag)将XML转化为对象(objects)
HttpService_pb12.5
flex通过Http和WebService与服务器通信交互,交互后台采用java的Struts2做为处理后台.
NULL 博文链接:https://topmanopensource.iteye.com/blog/425794
flex 解析xml文件,利用httpservice读取xml文件,按要求过滤字符串,获取对应的值
帶有ASP.NET的Flex HTTPService 本教程旨在演示在帶有ASP.NET的Flex 2+中HTTPService功能的使用。 Adobe Flex中有許多方法可以與ASP.NET(或簡單地說,服務器端語言)進行通信。 HttpService,WebService,WebORB...
flex 与后台交互 httpservice 实例