AFNetworking 2.0 发送 Post 带有字典参数数组的请求
AFNetworking 2.0 Send Post Request with array of dictionary Parameters
我想在下面的 API 中尝试使用 post 参数,但是参数没有正确传递,收到的响应显示需要数据。那么谁能帮忙解决这个问题呢。
我的 url 是
forapp.com/api/getContacts.php?data=[
{"name":"abc","phone":"1234567890"},{"name":"Kate
Bell","phone":"9925992599"} ]
那么我如何将这种类型的请求传递给 api
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"name": @"hello",
@"phone": @"1234567890"};
NSLog(@"Dict %@",params);
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager POST:@"http://forapp.com/api/getContacts.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
只需添加
第一步
// create the dictionary of {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} this
NSDictionary *params = @{@"name": @"hello",
@"phone": @"1234567890"};
第 2 步
//create the one array of this output [ {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} ]
NSMutableArray *arr = [NSMutableArray arrayWithObjects:params,nil];
第三步
// convert your object to JSOn String
NSError *error = nil;
NSString *createJSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:objectsInCart
options:NSJSONWritingPrettyPrinted
error:&error]
encoding:NSUTF8StringEncoding];
第四步
//create the another one dictionary for send the data for like data=[ {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} ]
NSDictionary *pardsams = @{@"data": createJSON};
第五步
// finally start your request
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSLog(@"Dict %@",pardsams);
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager POST:@"http://forapp.com/api/getContacts.php" parameters:pardsams success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
根据这个url
forapp.com/api/getContacts.php?data=[ {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} ]
你应该这样实现
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"name": @"hello",
@"phone": @"1234567890"};
NSLog(@"Dict %@",params);
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager POST:@"http://forapp.com/api/getContacts.php" parameters:@{@"data": @[params]} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
添加以下内容:
// httpClient is a subclass of AFHTTPRequestOperationManager or AFHTTPSessionManager
httpClient.requestSerializer = AFJSONRequestSerializer;
并检查您的 post 数组不包含任何其他对象。它只包括数组。
我想在下面的 API 中尝试使用 post 参数,但是参数没有正确传递,收到的响应显示需要数据。那么谁能帮忙解决这个问题呢。
我的 url 是
forapp.com/api/getContacts.php?data=[ {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} ]
那么我如何将这种类型的请求传递给 api
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"name": @"hello",
@"phone": @"1234567890"};
NSLog(@"Dict %@",params);
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager POST:@"http://forapp.com/api/getContacts.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
只需添加
第一步
// create the dictionary of {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} this
NSDictionary *params = @{@"name": @"hello",
@"phone": @"1234567890"};
第 2 步
//create the one array of this output [ {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} ]
NSMutableArray *arr = [NSMutableArray arrayWithObjects:params,nil];
第三步
// convert your object to JSOn String
NSError *error = nil;
NSString *createJSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:objectsInCart
options:NSJSONWritingPrettyPrinted
error:&error]
encoding:NSUTF8StringEncoding];
第四步
//create the another one dictionary for send the data for like data=[ {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} ]
NSDictionary *pardsams = @{@"data": createJSON};
第五步
// finally start your request
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSLog(@"Dict %@",pardsams);
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager POST:@"http://forapp.com/api/getContacts.php" parameters:pardsams success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
根据这个url
forapp.com/api/getContacts.php?data=[ {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} ]
你应该这样实现
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"name": @"hello",
@"phone": @"1234567890"};
NSLog(@"Dict %@",params);
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager POST:@"http://forapp.com/api/getContacts.php" parameters:@{@"data": @[params]} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
添加以下内容:
// httpClient is a subclass of AFHTTPRequestOperationManager or AFHTTPSessionManager
httpClient.requestSerializer = AFJSONRequestSerializer;
并检查您的 post 数组不包含任何其他对象。它只包括数组。