我有一个IOS应用程序的后端。我正在尝试使用JSON从rails后端读取数据。我的bubbleWrap get请求如下。
BW::HTTP.get("url_here/children/1.json") do |response|
json = BW::JSON.parse response.body.to_str
for line in json
p line[:name]
end
end它没有带回任何数据,它实际上破坏了我的代码。我找不到任何关于如何使用rubymotion/Bubblewrap中的REST并将数据拉回我的应用程序的文档。
任何帮助都是非常感谢的。
发布于 2013-08-23 18:13:36
下面是我在很多应用程序中使用的一个方便的类抽象.为了分离关注点,它将API调用逻辑从视图控制器逻辑中完全抽象出来,并在马特·格林 2013年视察 talk之后进行了大量建模。
class MyAPI
APIURL = "http://your.api.com/whatever.json?date="
def self.dataForDate(date, &block)
BW::HTTP.get(APIURL + date) do |response|
json = nil
error = nil
if response.ok?
json = BW::JSON.parse(response.body.to_str)
else
error = response.error_message
end
block.call json, error
end
end
end那么,要调用这个类,我们需要:
MyAPI.dataForDate(dateString) do |json, error|
if error.nil?
if json.count > 0
json.each do |cd|
# Whatever with the data
end
else
App.alert("No Results.")
end
else
App.alert("There was an error downloading data from the server. Please check your internet connection or try again later.")
end
endhttps://stackoverflow.com/questions/18389461
复制相似问题