Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在url(iOS)中发送JSONArray参数时出错_Ios_Json_Nsarray_Afnetworking 2_Jsonkit - Fatal编程技术网

在url(iOS)中发送JSONArray参数时出错

在url(iOS)中发送JSONArray参数时出错,ios,json,nsarray,afnetworking-2,jsonkit,Ios,Json,Nsarray,Afnetworking 2,Jsonkit,我需要将数组转换为json,并将其作为url中的参数与其他参数一起发送。我使用jsonKit进行json转换,使用AFNetworking进行网络通信。但在服务器上,json中总是出现异常 以下是用于子转换的代码: NSString *jsonData = [self.finalArray JSONString]; NSString *data = [NSString stringWithFormat:@"%s",[jsonData UTF8String] ]; 此数据最终作为url中的参数发

我需要将数组转换为json,并将其作为url中的参数与其他参数一起发送。我使用jsonKit进行json转换,使用AFNetworking进行网络通信。但在服务器上,json中总是出现异常

以下是用于子转换的代码:

NSString *jsonData = [self.finalArray JSONString];
NSString *data = [NSString stringWithFormat:@"%s",[jsonData UTF8String] ];
此数据最终作为url中的参数发送。 网络通信代码:

postData=[postData stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSURL *requestUrl = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@%@",url,postData]];
NSURLRequest *request =[[NSURLRequest alloc] initWithURL:requestUrl];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

operation.responseSerializer = [AFJSONResponseSerializer serializer];
operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];

[operation setCompletionBlockWithSuccess:success failure:failure];
[operation start]; 
也用于以下转义字符:

[postData stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]

收到错误:

Error Domain=NSURERRORDOMAIN Code=-1002“不支持的URL”UserInfo={NSUnderlyingError=0x7d51e9a0{Error Domain=KCFERRORRDOMAINCFNET Code=-1002“不支持的URL”UserInfo={NSLocalizedDescription=不支持的URL},NSLocalizedDescription=不支持的URL}


我想你应该这样使用它

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString *strURL = [NSString stringWithFormat:@"%@/add-product.php", apiPrefix];

[manager POST:strURL parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictData options:0 error:&error]; // dictData is your dictionary
    NSAssert(jsonData, @"Failure building JSON: %@", error);

    NSDictionary *jsonHeaders = @{@"Content-Disposition" : @"form-data; name=\"data\""}; // data is your parameter name in which you have to pass the json

    [formData appendPartWithHeaders:jsonHeaders body:jsonData];
}

success:^(AFHTTPRequestOperation *operation, id responseObject)
{
    NSLog(@"JSON: %@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
    NSLog(@"Error: %@", error);
}];
AFHTTPRequestOperationManager*manager=[AFHTTPRequestOperationManager];
NSString*strURL=[NSString stringWithFormat:@“%@/add product.php”,apiPrefix];
[管理员职位:strURL参数:nil constructingBodyWithBlock:^(id formData)
{
n错误*错误;
NSData*jsonData=[NSJSONSerialization dataWithJSONObject:dictData选项:0错误:&错误];//dictData是您的字典
NSAssert(jsonData,@“构建JSON失败:%@”,错误);
NSDictionary*jsonHeaders=@{@“内容处置”:@“表单数据;名称=\“数据\”“};//数据是必须在其中传递json的参数名称
[formData appendPartWithHeaders:jsonHeaders正文:jsonData];
}
成功:^(AFHTTPRequestOperation*操作,id响应对象)
{
NSLog(@“JSON:%@”,responseObject);
}
失败:^(AFHTTPRequestOperation*操作,NSError*错误)
{
NSLog(@“错误:%@”,错误);
}];

希望这将有助于……

可能重复已经尝试过的解决方案。但它仍然给出了相同的错误。我认为问题在于将数组转换成JSONonce@Anbu.Karthik你能指出错误吗?如果能说明答案是如何解决问题的,答案会更有用。