gpt4 book ai didi

iphone - AFNetworking POST 已创建但空白

转载 作者:行者123 更新时间:2023-11-29 03:45:50 26 4
gpt4 key购买 nike

我正在构建一个 Rails 支持的 ios 应用程序,它使用 AFNetworking 将内容发布到服务器。用户可以上传带有评论的照片 - 这很有效。我还希望能够选择让用户只上传文本——这就是我遇到麻烦的地方。我有一种保存照片和文本的方法,还有另一种仅保存文本的方法。保存照片方法有效,但保存文本方法创建帖子但文本为空。保存照片的实现是这样的:

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

此方法仅在有照片数据时有效 - 如果没有照片数据,应用程序将崩溃。所以我想知道什么相当于 multipartFormRequest- 只允许包含一个字符串?这就是我现在所拥有的 - 它创建了一个 post- 但返回内容:以及应该与当前位置一起返回的 lat/lng 参数。这是在 post 模型中定义的

- (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 save 中调用:

- (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 协议(protocol),但我一直无法找到一种方法实现一个不需要 fileData 的 post 请求。如何发出不附加 fileData 的发布请求?任何帮助将不胜感激。谢谢!

最佳答案

您可以复制现有方法,但不使用 appendPartWithFileData:name:fileName:mimeType: 来设置文件数据,您可以将参数转换为数据并使用 appendPartWithFormData:name: 添加它们:

关于iphone - AFNetworking POST 已创建但空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17774898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com