我知道我们可以在没有 Rhosync 的情况下使用 rhodes 同步数据,或者通过使用直接 Web 服务来同步 Rhoconnect,但我在这里有点困惑在哪里放置用于 Web 服务调用的代码以及如何初始化它。谁能帮我举个小例子?
提前谢谢。
我明白了,它对我有用。
class ProductController < Rho::RhoController
include BrowserHelper
# GET /product
def index
response = Rho::AsyncHttp.get(:url => "example.com/products.json",
:headers => {"Content-Type" => "application/json"})
@result = response["body"]
render :back => '/app'
end
# GET /product/{1}
def show
id =@params['id']
response = Rho::AsyncHttp.get(:url => "example.com/products/"+ id +".json",
:headers => {"Content-Type" => "application/json"})
@result = response["body"]
end
# GET /product/new
def new
@product = product.new
render :action => :new, :back => url_for(:action => :index)
end
# GET /product/{1}/edit
def edit
id =@params['product_id'].to_s
response = Rho::AsyncHttp.get(:url => "example.com/products/#{id}.json",
:headers => {"Content-Type" => "application/json"})
@result = response["body"]
end
# POST /product/create
def create
name = @params['product']['name']
price = @params['product']['price']
body = '{"product" : {"name" : "'+ name +'","price" :"'+ price +'" } }'
@result = Rho::AsyncHttp.post(:url => "example.com/products.json",
:body => body, :http_command => "POST", :headers => {"Content-Type" => "application/json"})
redirect :action => :index
end
# POST /product/{1}/update
def update
name=@params['product']['name']
price=@params['product']['price']
body = '{"product" : {"name" : "' + name + '","price" :"' + price + '" } }'
id = @params["product_id"].to_s
response = Rho::AsyncHttp.post(:url => "example.com/products/#{id}.json",
:body => body, :http_command => "PUT",:headers => {"Content-Type" => "application/json"})
redirect :action => :index
end
# POST /product/{1}/delete
def delete
id = @params["product_id"].to_s
response = Rho::AsyncHttp.post(:url => "example.com/products/#{id}.json",
:http_command => "DELETE",
:headers => {"Content-Type" => "application/json"})
redirect :action => :index
end
end
最简单的 http 服务器请求形式是:
Rho::AsyncHttp.get(
:url => "http://www.example.com",
:callback => (url_for :action => :httpget_callback)
)
其中httpget_callback
是控制器回调方法的名称。
有关更多详细信息,请参阅官方 Rhodes 文档。