2012-12-12 57 views
0

我正在使用Rubymotion創建iOS應用程序。我正在嘗試製作圖像的分段上傳 。無法使用AFNetworking上傳圖片

我用這個代碼,以使上傳,但我得到的錯誤:

data = {token: "2xCGdzcuEeNzhst3Yaa8f", task: 1, message: "message", latitude: 1, longitude: 1} 

    client = AFHTTPClient.alloc.initWithBaseURL(NSURL.URLWithString("http://api.example.com/api/v1/")) 

     request = client.multipartFormRequestWithMethod('POST', path:"messages", parameters:data, constructingBodyWithBlock:lambda{ |form_data| 
      image_data = UIImagePNGRepresentation(image.image) 
      form_data.appendPartWithFileData(image_data, name:'new_avatar', fileName:'new_avatar.png', mimeType:'image/png') 
     }) 

     operation = AFJSONRequestOperation.alloc.initWithRequest(request) 
     operation.setCompletionBlockWithSuccess(lambda{ |operation, responseObject| puts 'all done!'}) 

我得到這個錯誤:

undefined method `setCompletionBlockWithSuccess' for #<AFJSONRequestOperation:0xb227f00> (NoMethodError) 

從我所看到的,操作是有效的對象並應該有這種方法。 我在這裏錯過了什麼嗎?

更新1

這是我更新的代碼,但是當我運行它似乎POST操作不火。我仍然缺少任何代碼?我也沒有得到任何錯誤。

data = {token: "2xCGdzcuEeNzhs5tYaa8f", task: 1, message: "message", latitude: 1, longitude: 1} 

    client = AFHTTPClient.alloc.initWithBaseURL(NSURL.URLWithString("http://api.example.com/api/v1/")) 

    request = client.multipartFormRequestWithMethod('POST', path:"messages", parameters:data, constructingBodyWithBlock:lambda{ |form_data| 
     image_data = UIImagePNGRepresentation(image.image) 
     form_data.appendPartWithFileData(image_data, name:'new_avatar', fileName:'new_avatar.png', mimeType:'image/png') 
    }) 

    operation = AFJSONRequestOperation.alloc.initWithRequest(request) 
    operation.setCompletionBlockWithSuccess(lambda { |operation, responseObject| puts 'all done!'}, 
              failure: lambda { |operation, error| puts 'error' }) 

請求對象輸出這樣的:

<NSMutableURLRequest:195641792 - url: http://api.example.com/api/v1/messages, 
    headers: {"Content-Length"=>"118200", "Content-Type"=>"multipart/form-data; boundary=Boundary+0xAbCdEfGbOuNdArY", "Accept-Language"=>"en, fr, de, ja, nl, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, en-GB, ca, hu, vi, en-us;q=0.8", "User-Agent"=>"theapp/1.0 (iPhone Simulator; iOS 6.0; Scale/1.00)"}, 
    cache policy: 0, Pipelining: false, main doc url: , timeout: 60.0, network service type: 0 > 

操作對象輸出這樣的:

<AFJSONRequestOperation:0xa78c660> 

回答

1

方法簽名是setCompletionBlockWithSuccess(lambda..., failure: lambda...) - 您需要添加失敗參數。事情是這樣的:

operation.setCompletionBlockWithSuccess(lambda { |operation, responseObject| puts 'all done!'}, 
             failure: lambda { |operation, error| puts 'error' }) 

而且,你要確保真正開始請求操作:

operation.start 

這種方法的定義見https://github.com/AFNetworking/AFNetworking/blob/master/AFNetworking/AFJSONRequestOperation.m#L102-L103

+0

謝謝!我現在擺脫了錯誤信息,但POST操作似乎沒有啓動。請看我更新的問題。 –

+0

你真的開始了這個請求嗎? 'operation.start' –

0

我寫了一個類來完成這樣的工作。看看我的回答HERE。它使用AFNetworking幷包含基本網絡請求的方法。

+0

謝謝,但不幸的是我使用Rubymotion(Ruby)而不是objective-c。任何想法,爲什麼我得到這個錯誤? –

+0

您可以在Rakefile中使用「pod」AFNetworking「'安裝CocoaPods(motion-cocoapod)並將AFNetworking包含在RubyMotion應用程序中。有關CocoaPods的更多信息,請訪問:https://github.com/HipByte/motion-cocoapods。 –

+0

是的,我將這個吊艙與AFmotion寶石一起使用。沒有辦法直接上傳圖片與該寶石,所以我正在嘗試原始的AF代碼。 –