在AFNetworking 3或4中,使用GET请求并获得响应对象后,可以通过以下方式获取responseString和responseData。
AFNetworking 3示例代码:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:@"https://example.com/api" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 获取responseString
NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Response String: %@", responseString);
// 获取responseData
NSData *responseData = responseObject;
NSLog(@"Response Data: %@", responseData);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"Error: %@", error);
}];
AFNetworking 4示例代码:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:@"https://example.com/api" parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 获取responseString
NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Response String: %@", responseString);
// 获取responseData
NSData *responseData = responseObject;
NSLog(@"Response Data: %@", responseData);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"Error: %@", error);
}];
在成功的回调中,可以使用responseObject
获取到响应的数据,然后根据需要进行转换成字符串或者直接使用原始的数据。