NSDictionary *dataDict = <data to send>;
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dataDict options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [NSString stringWithFormat:@"%@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]];
NSLog(@「json = %@", jsonData);
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURL *baseURL = [NSURL URLWithString:@「http://dev.aaaa.com/」];
NSLog(@「base URL = %@", baseURL);
AFHTTPSessionManager *sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL sessionConfiguration:config];
[sessionManager.requestSerializer setTimeoutInterval:500.0];
sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
sessionManager.responseSerializer = responseSerializer;
sessionManager.responseSerializer.acceptableContentTypes = [sessionManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
[sessionManager POST:@「register」 parameters:dataDict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//-------- here append images as form data -------------------------
[formData appendPartWithFileData:imageData name:@「image」 fileName:@「myimage.jpg" mimeType:@"image/jpeg"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
progressBlock(uploadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
successBlock(task, responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
faliureBlock(task, error);
}];
使用這可能會幫助你。
感謝您的回覆......但沒有奏效。它遇到這個錯誤「2017-07-17 14:05:10.021實現[1172:74340]失敗:請求失敗:不可接受的內容類型:文本/純」。我不需要添加像app_id,enctype等httpheaderfields – Jitendra
更改應用程序/ JSON文本/純然,並嘗試和你寫你想發送app_id和app_key在標題這就是爲什麼我補充說,如果你的API是不需要那個頭然後刪除它 – vp2698
我也試過這一個,但它產生相同的錯誤。我做了一些修改也適合其要求,但同樣的問題..郵遞員的快照....它會幫助你瞭解更多鏈接1)https://drive.google.com/file/d/0BweySDAy0UNja1FscGJVVjIwSVE/view?usp=共享鏈接2)https://drive.google.com/file/d/0BweySDAy0UNjNWZ6bnc3d0Rtc3M/view?usp=sharing – Jitendra