HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – obj-c AFNetworking 2.0 POST请求不起作用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
希望通过使用AFNetworking 2.0将一些数据(字符串和文件)发送到服务器.不知何故,POST请求的数据(对于一个forumlar)是不正确的,它看起来像是请求上的编码/序列化丢失.由于服务器无法使用我上传的数据.

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

我假设URL格式参数编码,必须设置.文档说明

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

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

@H_333_6@manager.requestserializer = [[AFhttprequestserializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters];

在我的源代码下面

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请求不起作用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。