2012-12-12 54 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代码。 –