shm*_*mim 5 php objective-c ios afnetworking afnetworking-2
我和这里讨论的问题有类似的问题.
我正在尝试将JSON发布到服务器.
这是应该工作的Objective-C代码,但不是.我在响应对象中得到一个空数组,不知道为什么:
AFHTTPRequestOperation * operation = [manager POST:uploadScriptUrl
parameters:mutableJSON
success:^(AFHTTPRequestOperation * operation, id responseObject) {
successBlock(operation, responseObject);
}
failure:^(AFHTTPRequestOperation * operation, NSError * error) {
failureBlock(operation, error);
}];
Run Code Online (Sandbox Code Playgroud)
这段代码(改编自我用来上传图片的代码)KIND OF的作品.我知道这绝对不是批准的方式:
AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
AFJSONRequestSerializer * requestSerializer = [AFJSONRequestSerializer serializer];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.requestSerializer = requestSerializer;
AFHTTPRequestOperation * operation = [manager POST:uploadScriptUrl
parameters:mutableJSON //passed in
constructingBodyWithBlock:^(id <AFMultipartFormData> formData) {
//Do nothing
}
success:^(AFHTTPRequestOperation * operation, id responseObject) {
successBlock(operation, responseObject);
}
failure:^(AFHTTPRequestOperation * operation, NSError * error) {
failureBlock(operation, error);
}];
Run Code Online (Sandbox Code Playgroud)
它将我的JSON发布到服务器,但在制定请求的过程中,JSON被破坏了.
以下是它的开始:
{
"key1": [
{
"dictionary1": {
"param1": "value1",
"param2": "value2",
"array1A": [
"value1",
"value2"
],
"array1B": [
"value1",
"value2"
]
}
}
]
Run Code Online (Sandbox Code Playgroud)
}
这是AFNetworking发送给服务器的内容:
{
"key1": [
{
"dictionary1": {
"array1A": [
"value1"
]
}
},
{
"dictionary1": {
"array1A": [
"value2"
]
}
},
{
"dictionary1": {
"array1B": [
"value1"
]
}
},
{
"dictionary1": {
"array1B": [
"value2"
]
}
},
{
"dictionary1": {
"param1": "value1"
}
},
{
"dictionary1": {
"param2": "value2"
}
}
]
Run Code Online (Sandbox Code Playgroud)
}
这是查尔斯为请求显示的内容.在服务器触摸数据之前,您可以看到请求中JSON结构的更改方式.

这是我在服务器上使用的PHP.现在很简单:
<?php
header('Content-type: application/json'); //Not sure if this is needed.
$json_string = json_encode($_POST);
header("HTTP/1.0 200 OK");
echo $json_string;
?>
Run Code Online (Sandbox Code Playgroud)
所以,所有这些都说,这是我的问题:
AFNetworking是否处理嵌套的JSON数组?在这个页面上, Mattt说:"你所描述的结构无法通过查询字符串编码来确定." 我正在使用POST,因此不涉及查询字符串.但也许POST数据存在限制?
我也很好奇为什么较长的AFNetworking呼叫包括constructingBodyWithBlock成功而较短的呼叫失败.但是,这个答案对我来说不那么重要.较长的方法几乎可以工作,如果它返回我发送的相同JSON,我会很乐意使用它.
谢谢大家!
我正在使用POST,因此不涉及查询字符串.但也许POST数据存在限制?
限制不在POST上,它是URL表单编码.我继续说你应该编码为JSON,你可以对你的经理进行以下配置更改:
manager.requestSerializer = [AFJSONRequestSerializer serializer];.
此外,除非您实际构建多部分请求,否则请勿使用该constructingBodyWithBlock方法的版本.
| 归档时间: |
|
| 查看次数: |
5885 次 |
| 最近记录: |