2013-07-21 75 views
0

我正在構建一個rails支持的ios應用程序,它使用AFNetworking將內容發佈到服務器。用戶可以通過評論上傳照片 - 並且這可以工作。我也想讓用戶上傳文本 - 這是我遇到問題的地方。我有一種方法來保存照片和文字,另一種方法是保存文本。保存照片方法的作品,但保存文本方法創建一個帖子,但文本爲空。 的保存照片的實現是這樣的:AFNetworking POST創建但空白

- (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]; 

這種方法只有當photoData-如果你沒有photoData,應用程序崩潰的作品。 所以我想知道什麼是multipartFormRequest的等價物,它只允許包含一個字符串? 這就是我現在所擁有的 - 它創建一個後 - 但返回的內容:以及應與當前位置一起返回的緯度/經度。 這是在型號後

- (void)savePostAtLocation:(CLLocation *)location 
    withBlock:(void (^)(CGFloat progress))progressBlock completion:(void (^)(BOOL success, NSError *error))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]requestWithMethod:@"POST" path:@"/posts" parameters:params]; 

    AFHTTPRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:postRequest]; 

     [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
     if (operation.response.statusCode == 200 || operation.response.statusCode == 201) { 
      NSLog(@"Created, %@", responseObject); 
      NSDictionary *updatedPost = [responseObject objectForKey:@"post"]; 
      [self updateFromJSON:updatedPost]; 
      [self notifyCreated]; 
      completionBlock(YES, nil); 
     } else { 
      completionBlock(NO, nil); 
     } 
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     completionBlock(NO, error); 
     }]; 

    [[APIClient sharedClient] enqueueHTTPRequestOperation:operation]; 
} 

而在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; 
} 

,從而創建一個職位,但屬性是空的事實,讓我覺得這個問題是隻需在NSURLRequest-,而且我沒有完全實現AFNetworking協議,但我一直沒能找到一種方法來實現不需要fileData的post請求。如何發出不附加fileData的發佈請求? 任何幫助將不勝感激。 謝謝!

+0

實際上其不會有什麼」工作?服務器期望收到什麼?真的不清楚你在問什麼。 – Wain

+0

@Wain創建一個帖子,但字典中的參數爲空。內容爲空以及lat和lng爲空。我將包含上述成功帖子中的日誌,以便您看到。 – grantvansant

+0

字典不能包含'nil'。如果你試圖設置'nil',你會得到一個異常。服務器希望接收什麼內容,是否需要提取POST參數? – Wain

回答

0

這是我如何得到它的工作: post.h

+ (void)createNoteAtLocation:(CLLocation *)location 
       withContent:(NSString *)content 
         block:(void (^)(Post *post))block; 

post.m

+ (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]; 
     }]; 
    } 
1

您可以複製您現有的方法,但不使用appendPartWithFileData:name:fileName:mimeType:來設置文件數據,您可以將參數轉換爲數據並將其添加到appendPartWithFormData:name: