ios - AFNetworking : upload image with other parameters
<p><p>首先,感谢您阅读这个问题。</p>
<p>我正在开发使用 JSON 网络服务的应用程序。在应用程序中,我需要使用带有一些参数的图像(配置文件图像)调用 web 服务。
我的 JSON 请求应与以下相同...</p>
<pre><code>{
"adduser": {
"firstname": "testFirstName",
"lastname": "testLastName",
"email": "[email protected]",
"projectids" : "1"
}
}
</code></pre>
<p>还有其他变量如 <code>profileimage</code> 用于上传图片。</p>
<p>我已经写了以下代码。</p>
<pre><code> NSDictionary *dictParameter = @{@"adduser": @{@"firstname": firstName, @"lastname":lastName,@"email":email, @"projectids":@"1"}};
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);
}];
;
</code></pre>
<p>以上代码记录以下错误。</p>
<pre><code>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.}
</code></pre>
<p>如果我使用以下代码,它就可以正常工作。但我无法上传图片。</p>
<pre><code>AFHTTPRequestOperationManager *manager = ;
[manager POST:strURL parameters:dictParameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
</code></pre>
<p>谢谢</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您缺少实际添加要发送到服务器的图像数据的部分。 </p>
<pre><code>NSDictionary *dictParameter = @{@"adduser": @{@"firstname": firstName, @"lastname":lastName,@"email":email, @"projectids":@"1"}};
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);
}];
;
</code></pre>
<p>查看关于 <a href="https://github.com/AFNetworking/AFNetworking#post-multi-part-request" rel="noreferrer noopener nofollow">posting a multi-part request</a> 的 AFNetworking 文档.</p></p>
<p style="font-size: 20px;">关于ios - AFNetworking : upload image with other parameters,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/20393712/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/20393712/
</a>
</p>
页:
[1]