2

我是一名 Objective-C 初学者,我尝试使用 RestKit 向服务发布一些内容

这是我要发布到 Web 服务的 json。

{
   "image":"images/cover.jpg"
   "text":"this is a cover pic"
}

我将发送一个 nsdata 格式的图像图像将保存一个路径,我可以获得一个显示图片的路径。

喜欢:ipAddress/images/cover.jpg

我定义了两个对象: TSImage 对象:

@interface TSImage : NSObject

@property(nonatomic, strong) NSData *image;

@property(nonatomic, copy) NSString *text;


@end

这是我创建帖子数据的代码

    TSImage *testing = [[TSImage alloc] init];
    NSURL *imageUrl = [NSURL URLWithString:@"https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-ash3/995399_571403339577816_2064708560_n.jpg"];
    UIImage *urlImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:imageUrl]];

    NSData *imageData = UIImageJPEGRepresentation(urlImage, 0.7);


    testing.image = imageData;
    testing.text = @"testingup";
    NSURL *baseURL = [NSURL URLWithString:@""]; //for security
    NSString* path = @"/image/";




  RKObjectMapping *objectMapping = [RKObjectMapping requestMapping];
  [objectMapping addAttributeMappingsFromArray:@[@"image",@"text"]];

  RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:objectMapping
                                                                                   objectClass:[TSImage class]
                                                                                   rootKeyPath:nil];
    RKObjectManager *manager = [RKObjectManager managerWithBaseURL:baseURL];
    [manager addRequestDescriptor:requestDescriptor];

然后我尝试了两种发布数据的方法

第一种方法:

[manager postObject:testing path:path parameters:nil
            success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
                    NSLog(@"Loading mapping result: %@", result);

                } failure:^(RKObjectRequestOperation *operation, NSError *error) {
                    RKLogError(@"Operation failed with error: %@", error);
                }];

我只将文本发布到服务器

{
   "image":""
   "text" "testingup"
}

第二种方式:

NSMutableURLRequest *request =
    [manager multipartFormRequestWithObject:testing method:RKRequestMethodPOST
                                             path:path parameters:nil
                        constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
     {
         [formData appendPartWithFileData:UIImageJPEGRepresentation(urlImage, 0.7)
                                     name:@"expense[photo_file]"
                                 fileName:@"photo.jpg"
                                 mimeType:@"image/jpeg"];
     }];
    RKObjectRequestOperation *operation =
    [manager objectRequestOperationWithRequest:request
                                             success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
     {
         // Success handler.
         NSLog(@"Loading mapping result: %@",mappingResult);
     } failure:^(RKObjectRequestOperation *operation, NSError *error) {
         // Error handler.
         RKLogError(@"Operation failed with error: %@", error);
     }];

我不知道为什么它没有发布。

这是我的参考RestKit

感谢您的收看。

4

2 回答 2

0

你在这里尝试做两件不同的事情。

第一个是简单地将两个字符串发布到服务器,第二个是使用 multipart/Form 发布一个字符串,一个图像。

确保您的后端服务器接受什么。

于 2013-07-30T04:41:50.173 回答
0

您必须开始操作:

[operation start];

或将其排入队列:

[manager enqueueObjectRequestOperation:operation];
于 2013-11-04T16:34:25.213 回答