2012-07-06 52 views
1

我已经检查出RestKit和GET工作RubyMotion >>如何进行POST?

@client.get("/?amount=5", delegate:self) 

有谁知道如何使一个POST和接收的结果呢?

的文档提到,它看起来是这样的 -

@client.post("/ask", params:@paramvar, delegate:self) 

你怎么封装@paramvar?我试过它作为一个数组,散列,甚至无 - 但是,他们都没有产生任何结果。

回答

3

在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 
6

看看发泡包装材料库。它包含一些非常好的HTTP帮助器。

http://bubblewrap.io/

+0

最新的BubbleWrap已经删除了已经废弃的BW :: HTTP库。现在使用AFMotion。 – 2015-03-17 04:28:49

0

来源: https://github.com/rubymotion/BubbleWrap

Insallation: -
在控制台运行 '宝石安装泡沫包装' 或提及 '宝石泡沫包装' 中的Gemfile

线要被添加在'app_delegate.rb'文件中(通过这个Bubblewrap API可以通过应用程序): -
要求'bubble-wrap/http'

语法示例代码: -
BW :: HTTP.get(“https://api.github.com/users/mattetti”,{credentials:{username:'matt',password:'aimonetti'}})do | response | p response.body.to_str#打印回复的正文 结束

相关问题