2013-07-30 62 views
2

我是一個Objective-C的初學者,我嘗試發佈的東西服務,RestKitRestKit 0.20郵政陣列的Json與NSData的

這是我想要發佈到Web服務的JSON。

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

我將發送的圖像與NSData的格式 圖像會保存,我可以得到一個路徑中可顯示圖片的路徑。

一樣:ip地址/圖片/ cover.jpg

我定義了兩個對象:TSImage對象:

@interface TSImage : NSObject 

@property(nonatomic, strong) NSData *image; 

@property(nonatomic, copy) NSString *text; 


@end 

這是我的代碼創建一個post數據

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); 
    }]; 

我卻不知道它爲什麼沒有張貼。

這是我reference RestKit

謝謝您的收看。

回答

0

你正試圖在這裏做兩件不同的事情。

第一個簡單地發佈兩個字符串到服務器,第二個發佈一個字符串,一個圖像,使用multipart/Form。

確保您的後端服務器具有哪些功能。

+0

其實我不知道爲什麼第二個沒有啓動 – Exiling

0

你要開始操作:

[operation start]; 

或排隊吧:

[manager enqueueObjectRequestOperation:operation];