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

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

问题描述

您好,想使用 AFNetworking 2.0 将一些数据(字符串和文件)发送到服务器.不知何故,POST 请求(对于论坛)的数据不正确,看起来请求的编码/序列化丢失了.由于服务器无法使用我上传的数据.

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

我假设必须设置 URL 表单参数编码.文档说明

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

我试图这样做,但我不知道如何正确地做到这一点.使用以下 Xcode 通过警告:

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

<块引用>

/.../CameraViewController.m:105:31:从NSMutableURLRequest *"分配给AFHTTPRequestSerializer *"的不兼容指针类型

在我的源代码下方:

CameraViewController.h

#import @interface CameraViewController : UIViewController @property (weak, nonatomic) IBOutlet UIImageView *imageView;@结尾

CameraViewControllerView.m

#import "CameraViewController.h"#import "AFHTTPRequestOperationManager.h"@interface CameraViewController()@property (nonatomic) int photoIsTaken;@结尾@implementation CameraViewController//删除了这个问题的不必要的代码-(无效)上传{NSLog(@"%s: 上传者", __FUNCTION__);AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];NSDictionary *parameters = @{@"latitude": @"8.444444",@"经度":@"50.44444",@"location": @"纽约",@"类型":@"2",@"索赔":@"纽约",@"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);} 失败:^(AFHTTPRequestOperation *operation, NSError *error) {NSLog(@"错误: %@, %@", error, operation.responseString);}];[自我解雇ViewControllerAnimated:没有完成:nil];}@结尾

解决方案

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

@interface AFURLConnectionOperation (AuthenticationChallengeUploadFix)@结尾@implementation AFURLConnectionOperation (AuthenticationChallengeUploadFix)- (NSInputStream *)connection:(NSURLConnection __unused *)connection needNewBodyStream:(NSURLRequest *)request {如果([request.HTTPBodyStream 符合协议:@protocol(NSCopying)]){返回 [request.HTTPBodyStream 副本];}返回零;}@结尾

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

解决方案

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天全站免登陆