我一直在检查 RestKit 和 GET 工作
@client.get("/?amount=5", delegate:self)
有谁知道如何进行 POST 并接收结果?
文档提到它看起来像这样 -
@client.post("/ask", params:@paramvar, delegate:self)
你封装了什么@paramvar?我已经将它作为数组、哈希甚至 nil 进行了尝试——但是,它们都没有产生任何结果。
我一直在检查 RestKit 和 GET 工作
@client.get("/?amount=5", delegate:self)
有谁知道如何进行 POST 并接收结果?
文档提到它看起来像这样 -
@client.post("/ask", params:@paramvar, delegate:self)
你封装了什么@paramvar?我已经将它作为数组、哈希甚至 nil 进行了尝试——但是,它们都没有产生任何结果。
看一下气泡包装库。它包括一些非常好的 HTTP 助手。
在 RubyMotion_Cookbook 中找到了一个示例。
https://github.com/IconoclastLabs/rubymotion_cookbook/tree/master/ch_8/06_sendinghttppost
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
@window.rootViewController = RootController.new
url_string = "http://rubymotion-cookbook.herokuapp.com/post"
post_body = "bodyParam1=BodyValue1&bodyParam2=BodyValue2"
url = NSURL.URLWithString(url_string)
request = NSMutableURLRequest.requestWithURL(url)
request.setTimeoutInterval(30)
request.setHTTPMethod("POST")
request.setHTTPBody(post_body.to_s.dataUsingEncoding(NSUTF8StringEncoding))
queue = NSOperationQueue.alloc.init
NSURLConnection.sendAsynchronousRequest(request,
queue: queue,
completionHandler: lambda do |response, data, error|
if(data.length > 0 && error.nil?)
html = NSString.alloc.initWithData(data, encoding: NSUTF8StringEncoding)
p "HTML = #{html}"
elsif( data.length == 0 && error.nil? )
p "Nothing was downloaded"
elsif(!error.nil?)
p "Error: #{error}"
end
end
)
@window.makeKeyAndVisible
true
end
end
来源: https ://github.com/rubymotion/BubbleWrap
安装:-
在控制台中运行“gem install bubble-wrap”或在 Gemfile 中提及“gem bubble-wrap”
要在 'app_delegate.rb' 文件中添加的行(此 Bubblewrap api 可通过应用程序获得):-
需要 'bubble-wrap/http'
语法示例代码:-
BW::HTTP.get(" https://api.github.com/users/mattetti ", {credentials: {username: 'matt', password: 'aimonetti'}}) 做 |response | p response.body.to_str # 打印响应的正文结尾