Iphone 已创建AFN网络帖子,但为空

Iphone 已创建AFN网络帖子,但为空,iphone,ios,post,afnetworking,Iphone,Ios,Post,Afnetworking,我正在构建一个rails支持的ios应用程序,它使用AFNetworking将内容发布到服务器。用户可以上传一张带有评论的照片,这很有效。我还希望有一个选项,让用户上传只是文字-这是我遇到的麻烦。我有一种保存照片和文本的方法,还有一种只保存文本的方法。save photo方法可以工作,但是save text方法创建了一个post,但是文本为null。 保存照片的实现如下所示: - (void)savePhotoAtLocation:(CLLocation *)location

我正在构建一个rails支持的ios应用程序,它使用AFNetworking将内容发布到服务器。用户可以上传一张带有评论的照片,这很有效。我还希望有一个选项,让用户上传只是文字-这是我遇到的麻烦。我有一种保存照片和文本的方法,还有一种只保存文本的方法。save photo方法可以工作,但是save text方法创建了一个post,但是文本为null。 保存照片的实现如下所示:

- (void)savePhotoAtLocation:(CLLocation *)location
                     withBlock:(void (^)(CGFloat))progressBlock completion:(void (^)(BOOL, NSError *))completionBlock {

if (!self.content) self.content = @"";

NSDictionary *params = @{
                         @"post[content]" : self.content,
                         @"post[lat]": @(location.coordinate.latitude),
                         @"post[lng]": @(location.coordinate.longitude)

                         };

NSURLRequest *postRequest = [[APIClient sharedClient] multipartFormRequestWithMethod:@"POST"                                                                               path:@"/posts" parameters:params 
    constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
                                          {
                                              [formData appendPartWithFileData:self.photoData
                                                                          name:@"post[photo]"
                                                                      fileName:@""
                                                                      mimeType:@"image/png"];
                                          }];
    AFHTTPRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:postRequest];
在AddPostViewController保存中调用以下命令:

- (void)save:(id)sender

{
    CLLocationManager * locationManager = [[CLLocationManager alloc] init];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    self.locationManager.distanceFilter = 80.0f;
    [locationManager startUpdatingLocation];
    [self getLocation];
    CLLocation * location = [locationManager location];

    Post *post = [[Post alloc] init];
    post.content = self.contentTextField.text;

    [self.view endEditing:YES];

    ProgressView *progressView = [ProgressView presentInWindow:self.view.window];
    if (location) {

    [post savePostAtLocation:self.locationManager.location withBlock:^(CGFloat progress) {
        [progressView setProgress:progress];
    } completion:^(BOOL success, NSError *error) {
        [progressView dismiss];
        if (success) {
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            NSLog(@"ERROR: %@", error);
        }
    }];
    } else {
        NSLog(@"No Location");
    }
}
这是创建帖子后的日志。正如您所看到的,属性是空的,不应该是空的

Created, {
    post =     {
        content = "<null>";
        "created_at" = "2013-07-21T18:45:12Z";
        id = 13;
        lat = "<null>";
        lng = "<null>";

    success = 1;
}
创建{
职位={
content=“”;
“创建于”=“2013-07-21T18:45:12Z”;
id=13;
lat=“”;
液化天然气=”;
成功=1;
}
因此,虽然创建了一个post,但属性为空,这一事实让我认为问题只存在于NSURLRequest中——我没有完全实现AFNetworking协议,但我还没有找到一种方法来实现一个不包含fileData的post请求。我如何生成一个不附加fileData的post请求? 任何帮助都将不胜感激。
谢谢!

您可以复制现有方法,但不必使用
appendPartWithFileData:name:fileName:mimeType:
来设置文件数据,您可以将参数转换为数据,并使用
appendPartWithFormData:name:
将其添加到文件数据中。我就是这样做的: 邮局

博士后

+ (void)createNoteAtLocation:(CLLocation *)location
                 withContent:(NSString *)content
                       block:(void (^)(Post *post))block
{
    NSDictionary *parameters = @{ @"post": @{
                                          @"lat": @(location.coordinate.latitude),
                                          @"lng": @(location.coordinate.longitude),
                                          @"content": content
                                          }
                                  };

    [[APIClient sharedClient] postPath:@"/posts" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        Post *post = [[Post alloc] initWithDictionary:responseObject];
        if (block) {
            block(post);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (block) {
            block(nil);
        }
    }];
}
最后在createPostViewController中:

- (void)save:(id)sender

{
    CLLocationManager * locationManager = [[CLLocationManager alloc] init];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    self.locationManager.distanceFilter = 80.0f;
    [locationManager startUpdatingLocation];

    [self getLocation];
    CLLocation * location = [locationManager location];


    Post *post = [[Post alloc] init];
    post.content = self.contentTextField.text;

    [self.view endEditing:YES];

    if (location) {

        [Post createNoteAtLocation:location withContent:self.contentTextField.text block:^(Post *post) {
            NSLog(@"Block: %@", post);
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }

什么实际不起作用?服务器希望收到什么?确实不清楚您在问什么。@Wain已创建一篇文章,但字典中的参数为空。内容为null以及lat和lng null。我将包含上面成功文章中的日志,以便您可以看到。字典不能包含
nil
。如果您ed设置
nil
你会得到一个异常。服务器希望收到什么,这是拾取POST参数的问题吗?@Wain是的。我实现了一个类别,该类别返回一个空值,以防止该异常,因为它可以更容易地找出lat/lng问题。但是,是的,这是拾取pos的问题t参数化您的category方法在避免异常时输出一个日志…那么服务器希望收到什么?您是否检查了服务器日志?如果不知道服务器希望得到什么参数名,则无法提供正确的参数。
+ (void)createNoteAtLocation:(CLLocation *)location
                 withContent:(NSString *)content
                       block:(void (^)(Post *post))block
{
    NSDictionary *parameters = @{ @"post": @{
                                          @"lat": @(location.coordinate.latitude),
                                          @"lng": @(location.coordinate.longitude),
                                          @"content": content
                                          }
                                  };

    [[APIClient sharedClient] postPath:@"/posts" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        Post *post = [[Post alloc] initWithDictionary:responseObject];
        if (block) {
            block(post);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (block) {
            block(nil);
        }
    }];
}
- (void)save:(id)sender

{
    CLLocationManager * locationManager = [[CLLocationManager alloc] init];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    self.locationManager.distanceFilter = 80.0f;
    [locationManager startUpdatingLocation];

    [self getLocation];
    CLLocation * location = [locationManager location];


    Post *post = [[Post alloc] init];
    post.content = self.contentTextField.text;

    [self.view endEditing:YES];

    if (location) {

        [Post createNoteAtLocation:location withContent:self.contentTextField.text block:^(Post *post) {
            NSLog(@"Block: %@", post);
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }