OStack程序员社区-中国程序员成长平台

标题: ios - AFNetworking : upload image with other parameters [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 17:00
标题: ios - AFNetworking : upload image with other parameters

首先,感谢您阅读这个问题。

我正在开发使用 JSON 网络服务的应用程序。在应用程序中,我需要使用带有一些参数的图像(配置文件图像)调用 web 服务。 我的 JSON 请求应与以下相同...

{
  "adduser": {
  "firstname": "testFirstName",
  "lastname": "testLastName",
  "email": "[email protected]",
  "projectids" : "1"
  }
  }

还有其他变量如 profileimage 用于上传图片。

我已经写了以下代码。

    NSDictionary *dictParameter = @{@"adduser": @{@"firstname": firstName, @"lastname":lastName,@"email":email, @"projectids""1"}};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    AFHTTPRequestOperation *op = [manager POST:strURL parameters:dictParameter constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        } success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"Error: %@ ***** %@", operation.responseString, error);
            }];
 [op start];

以上代码记录以下错误。

Error: <br />
<b>Notice</b>:  Undefined index: json in <b>/opt/lampp/htdocs/testproject/app/webroot/webservices/include.php</b> on line <b>15</b><br />
{"status":"failure","message":"Your Request is Empty"} ***** Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x8abe490 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

如果我使用以下代码,它就可以正常工作。但我无法上传图片。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
   [manager POST:strURL parameters:dictParameter success:^(AFHTTPRequestOperation *operation, id responseObject) {

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    }];

谢谢



Best Answer-推荐答案


您缺少实际添加要发送到服务器的图像数据的部分。

NSDictionary *dictParameter = @{@"adduser": @{@"firstname": firstName, @"lastname":lastName,@"email":email, @"projectids""1"}};

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestOperation *op = [manager POST:strURL parameters:dictParameter constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name"image" error:nil];
        } 
success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
            } 
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"Error: %@ ***** %@", operation.responseString, error);
            }];

 [op start];

查看关于 posting a multi-part request 的 AFNetworking 文档.

关于ios - AFNetworking : upload image with other parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20393712/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4