我仍然在使用ASIHTTPRequest,我正期待着迁移到AFNetworking,我也通过了Raywenderlich Crash Course,但它没有使用AFNetworking 2.0
我刚刚尝试了下面的示例,这是在AFNetworking提到的,但它没有工作的一些方式。
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
NSDictionary *parameters = @{@"UserId": @"24",@"ArticleId":@"0"};
NSLog(@"%@",parameters);
[manager POST:@"http://mysite.com/api/User/showArticleList" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"JSON: %@", responseObject);
}failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error: %@", error);
}];
调试区域显示:
误差Domain=NSCocoaErrorDomain Code=3840
“操作无法完成。(可可误差3840)。”(JSON文本没有以数组或对象和选项开头,以允许不设置片段。)UserInfo=0xa0ba580 {NSDebugDescription=JSON文本没有以数组或对象和选项开头,以允许不设置片段。}
但是当我使用上面提到的链接时,Raywenderlich速成班
[manager POST:@"http://www.raywenderlich.com/downloads/weather_sample/weather.php?format=json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"JSON: %@", responseObject);
}failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error: %@", error);
}];
它给了我完美的JSON输出,为什么呢?
发布于 2013-12-26 16:54:44
我终于找到了如下的解决办法-
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"UserId": @"24",@"Name":@"Robin"};
NSLog(@"%@",parameters);
parameters = nil;
// if you want to sent parameters you can use above code
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:@"http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"JSON: %@", responseObject);
}failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error: %@", error);
}];
对于text/Html +,如果它没有提供正确的JSON字符串,可以从字符串中删除它,并将其转换为数组或字典。
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
// if you want to sent parameters you can use above code
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
// header("Content-Type: application/json");
// manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:@"your url" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"responseObject %@",responseObject);
NSString *jsonString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSString *newJsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\'" withString:@""];
/*
NSRange range = [jsonString rangeOfString:@"}" options:NSBackwardsSearch];
jsonString = [jsonString substringToIndex:range.location + 1];
*/
NSData *data = [newJsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(@"array %@",array);
if (!array) {
NSLog(@"Parsing JSON failed: %@", error);
}
/*
NSData *newJSONData = [newJsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:newJSONData
options:NSJSONReadingMutableContainers
error:&error];
NSLog(@"json %@",json);
*/
NSLog(@"responseObject = %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",[error description]);
}];
在某些情况下,您需要更改响应字典/数组,但有时对象的所有片段都不是可变的。
为了做到这一点,请如下所示。
辞典
NSError *error;
NSData *dataFromDict = [NSJSONSerialization dataWithJSONObject:responce options:NSJSONWritingPrettyPrinted error:&error];
responseDictionary = [[NSMutableDictionary alloc]init];
responseDictionary = [NSJSONSerialization JSONObjectWithData:dataFromDict options:NSJSONReadingMutableContainers error:&error];
阵列
NSError *error;
NSData *dataFromDict = [NSJSONSerialization dataWithJSONObject:responce options:NSJSONWritingPrettyPrinted error:&error];
responseArray = [[NSMutableDictionary alloc]init];
responseArray = [NSJSONSerialization JSONObjectWithData:dataFromDict options:NSJSONReadingMutableContainers error:&error];
发布于 2013-10-04 17:53:31
您似乎在服务器端有一个ASP.NET Web服务。默认情况下,它返回XML。
你有两个选择:
Accept: application/json
与请求一起发送。https://stackoverflow.com/questions/19186754
复制相似问题