obj-c AFNetworking 2.0 POST请求不起作用 [英] obj-c AFNetworking 2.0 POST request does not work

查看:115
本文介绍了obj-c AFNetworking 2.0 POST请求不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用AFNetworking 2.0将一些数据(字符串和文件)发送到服务器.某种程度上,POST请求的数据(对于论坛而言)不正确,看起来请求上的编码/序列化丢失了.由于服务器无法处理我上传的数据.

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.

如何为请求设置编码/序列化?

我假设必须设置URL形式参数编码.文档指出

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

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

我试图这样做,但是我不知道该怎么做.通过以下Xcode警告:

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:从"NSMutableURLRequest *"分配给"AFHTTPRequestSerializer *"的指针类型不兼容

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

在我的源代码下面:

#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

推荐答案

感谢@NobleK,类别可能是解决此问题的最佳方法.这是示例代码:

Thanks @NobleK , a category may be the best way to fix this issue. Here is a sample code:

@interface AFURLConnectionOperation (AuthenticationChallengeUploadFix)
@end

@implementation AFURLConnectionOperation (AuthenticationChallengeUploadFix)

- (NSInputStream *)connection:(NSURLConnection __unused *)connection needNewBodyStream:(NSURLRequest *)request {
    if ([request.HTTPBodyStream conformsToProtocol:@protocol(NSCopying)]) {
        return [request.HTTPBodyStream copy];
    }
    return nil;
}

@end

这篇关于obj-c AFNetworking 2.0 POST请求不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆