简体   繁体   English

AFNetworking 2 GET与JSON响应

[英]AFNetworking 2 GET with JSON response

I am building a simple GET function that should return a JSON object. 我正在构建一个简单的GET函数,该函数应返回JSON对象。 Working with AFNetworking 2. The code compiles, but i can not pinpoint the source of the error i get during runtime. 使用AFNetworking 2.代码可以编译,但是我无法查明运行时出现的错误源。 Could it be that the content-type: on the server is text/html and I expect jsonobj. 可能是服务器上的content-type:是text / html,我希望是jsonobj。 The server uses "Basic user:id" header, should I add this to setValue forHttpHeaderField? 服务器使用“ Basic user:id”标头,是否应将其添加到setValue forHttpHeaderField中?

    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://the website.com"]];
    manager.securityPolicy.allowInvalidCertificates = YES;
    [manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
    [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"user" password:@"pass"];   
    [manager GET:@"/api/data/interval?month" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@" Yep %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@" Nope %@", error);
    }];

And here is the error that I get 这是我得到的错误

 Nope Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: forbidden (403)" UserInfo=0x8a9dec0 {NSErrorFailingURLKey=http://thewebsite.com/api/data/interval?month, NSLocalizedDescription=Request failed: forbidden (403), NSUnderlyingError=0x8a99e00 "Request failed: unacceptable content-type: text/html", AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8bb0ac0> { URL: http://thewebsite.com/api/data/interval?month } { status code: 403, headers {
    Connection = "keep-alive";
    "Content-Encoding" = gzip;
    "Content-Language" = "";
    "Content-Type" = "text/html";
    Date = "Tue, 04 Feb 2014 13:02:12 GMT";

First you have to init your base url.Something like: 首先,您必须初始化您的基本URL,如下所示:

 NSURL *baseURL = [NSURL URLWithString:@"www.abv.bg"];

After that you create relative path like: 之后,您可以创建相对路径,例如:

NSString *path = @"/Path/to/somewhere";

And finally init URL string like this: 最后是这样的初始化URL字符串:

NSString *absoluteURL = [[NSURL URLWithString:path relativeToURL:baseURL] absoluteString];

And now you can execute request, and you know, that URL is correct: 现在您可以执行请求了,您知道URL是正确的:

[manager GET:absoluteURL parameters:nil  success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@" Yep %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@" Nope %@", error);
}];

In your solution you try to execute request to address @"/api/data/interval?month" and not to real URL. 在您的解决方案中,您尝试执行请求以寻址@“ / api / data / interval?month”而不是真实URL。 HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM