【问题标题】:Do AFJsonRequest and AFHttpClient work together?AFJsonRequest 和 AFHttpClient 一起工作吗?
【发布时间】:2012-04-07 13:20:38
【问题描述】:
    NSURL *url = [NSURL URLWithString:@"contact.php"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                        @"companyID", constCompanyID,
                        nil];

NSURLRequest *request = [httpClient requestWithMethod: @"POST" path:@"contact.php" parameters:params];


AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    for(id entry in JSON)
    {
        self.strAddr = [entry valueForKeyPath:@"addr"];
        CCLOG(@"Address: %@", self.strAddr);
    }
} failure:nil];
[operation start];
CCLOG(@"Address: %@", strAddr);

在日志中,地址为空。 PHP 很好;在浏览器中,它返回我想要的。

在我添加 AFHTTPClient 以传入变量之前,此代码一直有效。我一定是做错了什么,或者我不能那样做吗?

【问题讨论】:

    标签: ios json afnetworking


    【解决方案1】:

    在第 1 行,您尝试从“contact.php”(NSURL +URLWithString: 返回 nil)创建一个 URL,而您真正需要的是基本 URL——对特定 Web 服务的请求,例如“http://api.twitter.com/1/”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-20
      • 2012-01-17
      • 2013-07-09
      • 2016-01-29
      • 2017-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多