簡體   English   中英

AFNetworking 3.0沒有從響應中得到json

[英]AFNetworking 3.0 not getting json from response

我正在發布請求獲取響應responseObject但是在數據格式中。為什么它不是以json格式出現的。

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSString *urlstr=[SERVER_API_URL stringByAppendingString:methodType];
    [manager POST:urlstr parameters:input progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"success! data=%@",responseObject);
        NSLog(@"responseObject==%@",responseObject);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error: %@", error);
    }]

;

這里是我在NSLog中獲得的responseObject

success! data=<227b2261 63636573 735f746f 6b656e22 3a224d58 31563255 736c3053 47367654 706f2d4d 76346459 672d3369 35684c4e 796d4245 4e674e4d 56574b5a 6c394476 3937536e 364a4e7a 3952524b 556d4f50 717a485f 7748416e 63675849 70626e43 35703647 4c4f324d 7a773761 6b366d4d 6c4b6465 36766d62 3769434a 4973336c 3544674a 74345a4f 48523338 786c5270 554a4e56 5762356c 39347067 72417978 34555a47 35376444 456a4c62 39394d4b 55517358 546a3933 3751614e 526f5073 4c437859 4b5a7a6e 79483533 4f553555 4266457a 6f396732 5f767262 31736278 58334174 63467072 4a674d70 45682d45 33613750 346d3344 2d64706a 5a575365 7a4a6155 74463256 5a463733 6c716c32 5933222c 22746f6b 656e5f74 79706522 3a226265 61726572 222c2265 78706972 65735f69 6e223a38 36333939 7d22>

當我從郵遞員客戶傳遞請求時,我得到了所有正確的數據。 這是屏幕截圖。 在此輸入圖像描述

按以下方式更改您的代碼:

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
//manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];

NSString *urlstr=[SERVER_API_URL stringByAppendingString:methodType];
[manager POST:urlstr parameters:input progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSLog(@"[success]: %@",responseObject);

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"[error]: %@", error);
}]

設置可接受的內容類型

[manager.responseSerializer setAcceptableContentTypes:[NSSet setWithArray:@[@"application/json"]]];

然后將響應轉換為NSDictionary

NSDictionary *response = responseObject;
NSLog("api response: %@", response);

記得處理任何錯誤。


附注:如果您使用的是自簽名SSL,則可以通過以下方式允許無效證書:

manager.securityPolicy.allowInvalidCertificates = YES;
manager.securityPolicy.validatesDomainName = NO;

我得到了相同的錯誤,但是y修復它表明JSON將要打印的位置

在我之前

NSLog(@"JSON usando AFNetworking: %@",responseObject);

現在它合作

NSLog(@"JSON usando AFNetworking: %@",[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM