我需要发送普通的HTTP请求(GET),并在text/html中回答will。如何使用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: method,但作为响应,我看到了以下错误:
错误:错误Domain=AFNetworkingErrorDomain代码=-1016“请求失败:不可接受的内容类型: text/html”
请,任何人都可以解释我哪里出了问题,以及发送请求的正确方式是什么(如果我会以text/html的形式得到响应)?
你好,亚历克斯。
发布于 2013-11-09 14:49:32
你在评论中说,作为对使用AFHTTPRequestOperationManager的建议的回应
当我使用GET时,我得到了如下错误: Error: error Domain=AFNetworkingErrorDomain代码=-1016“请求失败:不可接受的内容类型: text/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-09 13:34:32
//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);
}
];https://stackoverflow.com/questions/19875886
复制相似问题