Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

ios - obj-c AFNetworking 2.0 POST request does not work

Hi want to send some data (strings and a file) to a server, by using AFNetworking 2.0. Somehow the data for the POST request (for a forumlar) ist not correct, it looks like that the encoding/serialization on the request is missing. As the server can't work with the uploaded data from me.

How do I set the encoding/serialization to the request?

I assume the URL Form Parameter Encoding, has to be set. The docs states

[[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters];

I tried to do that, but I cannot figure out how to do it right. With the following Xcode throughs a warning:

manager.requestSerializer = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters]; 

/.../CameraViewController.m:105:31: Incompatible pointer types assigning to 'AFHTTPRequestSerializer *' from 'NSMutableURLRequest *'

Below my sourcecode:

CameraViewController.h

#import <UIKit/UIKit.h>

@interface CameraViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

CameraViewControllerView.m

#import "CameraViewController.h"
#import "AFHTTPRequestOperationManager.h"

@interface CameraViewController ()    
@property (nonatomic) int photoIsTaken;    
@end

@implementation CameraViewController

// removed unecessary code for this question

- (void)upload {
    NSLog(@"%s: uploader ", __FUNCTION__);
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    NSDictionary *parameters = @{@"latitude": @"8.444444",
                                 @"longitude": @"50.44444",
                                 @"location": @"New York",
                                 @"type": @"2",
                                 @"claim": @"NYC",
                                 @"flag": @"0",
                                 @"file": UIImageJPEGRepresentation(self.imageView.image,0.2)};

NSString *URLString = @"http://192.168.1.157/tapp/laravel/public/foobar/upload";

manager.requestSerializer = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters];

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

    [self dismissViewControllerAnimated:NO completion:nil];
}

@end
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Finally it works. Was a hassle but now I am really happy... During my testing I had some problems with 'request body stream exhausted' within Wifi, what was strange.

Below the code that did the trick for me.

- (void)upload {

    // !!! only JPG, PNG not covered! Have to cover PNG as well
    NSString *fileName = [NSString stringWithFormat:@"%ld%c%c.jpg", (long)[[NSDate date] timeIntervalSince1970], arc4random_uniform(26) + 'a', arc4random_uniform(26) + 'a'];
    // NSLog(@"FileName == %@", fileName);

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    NSDictionary *parameters = @{@"lat": @"8.444444",
                                 @"lng": @"50.44444",
                                 @"location": @"New York",
                                 @"type": @"2",
                                 @"claim": @"NYC",
                                 @"flag": @"0"};
     // BASIC AUTH (if you need):
    manager.securityPolicy.allowInvalidCertificates = YES;
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"foo" password:@"bar"];
    // BASIC AUTH END

    NSString *URLString = @"http://192.168.1.157/tapp/laravel/public/foobar/upload";

    /// !!! only jpg, have to cover png as well
    NSData *imageData = UIImageJPEGRepresentation(self.imageView.image, 0.5); // image size ca. 50 KB
    [manager POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/jpeg"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure %@, %@", error, operation.responseString);
    }];

    [self dismissViewControllerAnimated:NO completion:nil];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...