如何将编码/序列化设置为请求?
我假设URL格式参数编码,必须设置.文档说明
[[AFhttpRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters];
我试图这样做,但我不知道如何做到正确.通过以下Xcode通过警告:
manager.requestSerializer = [[AFhttpRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters];
/…/CameraviewController.m:105:31: Incompatible pointer types assigning to ‘AFhttpRequestSerializer *’ from ‘NSMutableURLRequest *’
在我的源代码下面
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解决方法 最后它工作.是一个麻烦,但现在我真的很开心…在我的测试期间,我有一些问题,“请求身体流量耗尽”在Wifi,什么是奇怪的.
在为我做的伎俩的代码下面.
- (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",@"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 %@,operation.responseString); }]; [self dismissVIEwControllerAnimated:NO completion:nil];}总结
以上是内存溢出为你收集整理的ios – obj-c AFNetworking 2.0 POST请求不起作用全部内容,希望文章能够帮你解决ios – obj-c AFNetworking 2.0 POST请求不起作用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)