gpt4 book ai didi

ios - 如何通过 AFNetworking 创建简单的 Http 请求

转载 作者:技术小花猫 更新时间:2023-10-29 11:20:13 36 4
gpt4 key购买 nike

我仍在使用 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);
}];

调试区显示:

错误域=NSCocoaErrorDomain 代码=3840
“操作无法完成。(Cocoa 错误 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 输出,为什么会这样?

最佳答案

我终于找到了解决方案如下-

 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);
}];

对于文本/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];

关于ios - 如何通过 AFNetworking 创建简单的 Http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19186754/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com