2015-03-19 52 views
0

我有一些數據喜歡(圖片&文本)。我想通過我的客觀c編碼使用AFNetworking將這些數據發送到PHP服務器。如何使用iOS中的AFNetworking將數據發送到php服務器

我的回答是這樣的:

enter image description here

我要送指定&約字段文本到PHP服務器。

我的代碼是象下面這樣:

- (void) upload 
    { 
     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
     manager.requestSerializer = [AFJSONRequestSerializer serializer]; 

     NSDictionary *params = @ {@"about" :self.aboutField.text, @"designation" :self.desinationField.text }; 

     [manager POST:@UPDATE_PROFILE_URL parameters:params 
     success:^(AFHTTPRequestOperation *operation, id responseObject) 
     { 
     NSLog(@"JSON: %@", responseObject); 
     } 
     failure: 
     ^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %@", error); 
     }]; 
    } 

,我在AFURLResponseSerialization類中添加 「text/html的」 以acceptableContentTypes集。

當我跑我的應用我有以下錯誤:

Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x7d405870 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} 

請幫助我。

+0

您設置的請求序列化JSON,但我想你談談responseSerializer。 – Andy 2015-03-19 10:44:05

回答

0

上傳圖片到服務器最後我能夠使用AFNetworking上傳圖片&文本服務器上。

添加這2類AFNetworking & 的UIKit + AFNetworking

導入:

#import "AFHTTPRequestOperation.h" 
#import "AFHTTPRequestOperationManager.h" 

這是我的代碼:

- (void)uploadProfile_Details 
{ 
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@UPDATE_PROFILE_URL]]; 

    // Retrieve Image from Document directory------ 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"profileImg.png"]; 
    UIImage *img = [UIImage imageWithContentsOfFile:getImagePath]; 

    NSData *imageData = UIImageJPEGRepresentation(img, 0.5); 

    // Create dictionary for sending text to server ------ 

    NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:self.desinationField.text,@"designation",self.teamField.text,@"team",self.aboutField.text,@"about",self.skillLbl.text,@"skills",self.interestField.text,@"interests", nil]; 
    AFHTTPRequestOperation *operation = [manager POST:@UPDATE_PROFILE_URL parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) 
    { 
     //do not put image inside parameters dictionary as I did, but append it! 
     [formData appendPartWithFileData:imageData name:@"image" fileName:@"myimage.jpg" mimeType:@"image/jpeg"]; 
    } success:^(AFHTTPRequestOperation *operation, id responseObject)  { 
    NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject); 

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %@ ***** %@", operation.responseString, error); 
}]; 
[operation start]; 
} 
相關問題