【问题标题】:Passing NSData to web service along with NSDictionary Parameter将 NSData 与 NSDictionary 参数一起传递给 Web 服务
【发布时间】:2016-03-26 16:17:29
【问题描述】:

我所有的 AFNetworking 服务都是用单独的 web 服务类编写的。到目前为止,我在传递 NSDictionary 参数方面做得很好。但是现在当我需要将 NSData 文件传递​​给 Web 服务时遇到了问题。

我是如何执行网络服务的

 NSData *imageData = UIImageJPEGRepresentation(self.profileImg.image, 0.5); // I need to pass this imageData

    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

    NSString *stringFromDate = [dateFormatter stringFromDate:[NSDate date]];

    //Encrypting Password
    NSString *passwordString = _password.text;
    NSString *passwordMD5 = [passwordString MD5String];

    NSDictionary *params = @{@"username": _username.text,
                             @"password": passwordMD5,
                             @"email": _email.text,
                             @"date":stringFromDate};

    WebService *serviceObj = [[WebService alloc] init];
    serviceObj.delegate = self;
    [serviceObj performSelectorInBackground:@selector(doRegister:) withObject:params];

这里我是如何编写网络服务的

NSMutableDictionary * parameters = [[NSMutableDictionary alloc]initWithDictionary:params];

NSURL *baseURL = [NSURL URLWithString:@"http://www.example.com/register.php"];

AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

[manager POST:@"" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

    [formData appendPartWithFileData:imageData name:@"image" fileName:@"profile.png" mimeType:@"image/png"];

} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

    [delegate didReceiveRegisterResponse:responseObject];

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

}];

但我现在不知道如何分配该图像数据。

【问题讨论】:

    标签: ios objective-c afnetworking


    【解决方案1】:

    我已经在数组中传递了 NSData 和 NSDictionary。如下所示

    [self performSelectorInBackground:@selector(reloadPage:)
                           withObject:[NSArray arrayWithObjects:pageIndex,firstCase,nil] ];
    
    
    - (void) reloadPage: (NSArray *) args {
        NSString *pageIndex = [args objectAtIndex:0];    
        NSString *firstCase = [args objectAtIndex:1];    
    }
    

    question得到答案

    【讨论】:

      猜你喜欢
      • 2019-05-19
      • 1970-01-01
      • 2016-09-25
      • 1970-01-01
      • 2011-07-31
      • 2015-08-10
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多