2012-07-06 22 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#打印回覆的正文 結束

相關問題