4

我需要发送普通的 HTTP 请求 (GET) 并以 text/html 形式回答。如何使用 AFNetworkin 2 发送此响应?

现在我正在尝试使用

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://example.com"]];
[self HTTPRequestOperationWithRequest:request
                              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                  NSLog(@"JSON: %@", responseObject);
                              } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                  NSLog(@"Error: %@", error);
                              }];

并且很沮丧 - 它什么都不做。调试时,也没有触发成功或失败子句。

我也尝试使用 GET:parameters:success:failure: 方法,但作为响应我看到了这个错误:

错误:错误域=AFNetworkingErrorDomain 代码=-1016“请求失败:不可接受的内容类型:文本/html”

拜托,任何人都可以向我解释什么是错误的以及发送请求的正确方法是什么(如果我会得到文本/html的响应)?

问候,亚历克斯。

4

2 回答 2

17

您在评论中说,作为对使用的建议的回应AFHTTPRequestOperationManager

当我使用 GET 时,我收到了上面写的这个错误:错误:错误域=AFNetworkingErrorDomain 代码=-1016“请求失败:不可接受的内容类型:文本/html”

您可以使用以下方法解决此问题AFHTTPResponseSerializer

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:@"https://example.com" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // do whatever you'd like here; for example, if you want to convert 
    // it to a string and log it, you might do something like:

    NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"%@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

您还可以使用AFHTTPRequestOperation

NSOperationQueue *networkQueue = [[NSOperationQueue alloc] init];
networkQueue.maxConcurrentOperationCount = 5;

NSURL *url = [NSURL URLWithString:@"https://example.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    // do whatever you'd like here; for example, if you want to convert 
    // it to a string and log it, you might do something like:

    NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"%@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%s: AFHTTPRequestOperation error: %@", __FUNCTION__, error);
}];
[networkQueue addOperation:operation];

不过,理想情况下,建议编写返回 JSON(或 XML)的服务器代码,因为这样更容易让应用程序使用和解析。

于 2013-11-09T14:49:32.823 回答
1
//AFN 2.0 is just support IOS 7,and it's standard use as follow:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" 
  parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
               NSLog(@"JSON: %@", responseObject)
     }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"Error: %@", error);
     }
];
于 2013-11-09T13:34:32.447 回答