【发布时间】:2012-06-13 20:31:02
【问题描述】:
我正在编写一个需要将数据传递给基于 Grails 的 Web 应用程序的 Android 应用程序。我想在客户端使用 Post 请求。我不确定在服务器端从哪里开始。我知道我可能应该使用看起来像这样的代码:request.getParams() 但我不确定这需要去哪里。有什么建议吗?
【问题讨论】:
我正在编写一个需要将数据传递给基于 Grails 的 Web 应用程序的 Android 应用程序。我想在客户端使用 Post 请求。我不确定在服务器端从哪里开始。我知道我可能应该使用看起来像这样的代码:request.getParams() 但我不确定这需要去哪里。有什么建议吗?
【问题讨论】:
您需要创建一个controller。例如
FooController {
def index = {
if (request.status == 'POST')
print request.params
}
}
}
现在发帖子到http://<url context>/foo/index。
【讨论】:
在服务器端,无论请求是使用 HTTP 还是 HTTPS、GET 还是 POST,您的代码都是相同的。从控制器开始:
class TestController {
def index() {
// params is a map with all the request parameters
println params
// do your server side stuff here
}
}
对于开发,请将您的 android 应用程序发布到 http://<devserver>:8080/<appname>/test。部署到生产环境时,更改 URL https://<prodserver>/<appname>/test。
如果您想在开发中使用 HTTPS,您可以使用 grails run-app -https 运行开发服务器,grails 将自动创建自签名 SSL 证书并侦听端口 8443 上的 HTTPS 连接以及通常的 HTTP 服务器在 8080 端口上。
【讨论】: