【问题标题】:FourSquare request returns null string in iOSFourSquare 请求在 iOS 中返回空字符串
【发布时间】:2012-03-02 06:06:45
【问题描述】:

我到处寻找,似乎找不到解决方案。我正在使用 ASIHTTPRequest 在我的 iOS 应用程序中接入 FourSquare API。但是,当我尝试打印我希望返回给我的 JSON 字符串时,我得到“null”。如果我在浏览器中导航到相同的请求 URL,我会得到一大堆 JSON。这是我的代码...

- (void)fetchFoursquareLocationsUsingLocation:(CLLocation *)location {
// First, let's build our request
CLLocationDegrees latitude = location.coordinate.latitude;
CLLocationDegrees longitude = location.coordinate.longitude;
NSString *foursquareURLString = [NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?ll=%0.6f,%0.6f&client_id=%@&client_secret=%@&v=20120103", 
                                 latitude,
                                 longitude,
                                 FOURSQUARE_CLIENT_ID, 
                                 FOURSQUARE_CLIENT_SECRET];

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:foursquareURLString]];
self.foursquareData = [[NSMutableData alloc] init];

request.delegate = self;
[request startAsynchronous];
}

#pragma mark - ASIHTTPRequest Delegate
- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data {
[self.foursquareData appendData:data];

}

- (void)requestFinished:(ASIHTTPRequest *)request {
NSString *jsonCheck = [[NSString alloc] initWithData:self.foursquareData encoding:NSUTF8StringEncoding];

NSLog(@"%@", jsonCheck);
}

更新:感谢@Kamarshad 的 SO 帖子 How to get Venues list Using FourSquare Api,我能够取回有效的 JSON。不同之处在于我异步发出请求,这样

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:foursquareURLString]];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[NSURLConnection sendAsynchronousRequest:request 
                                   queue:queue 
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    //
    if (error != nil) {
        NSLog(@"Something went wrong...%@", [error localizedDescription]);
    }
    else {
        NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@", jsonString);
    }
}];

ASIHTTPRequest 阻止有效响应返回的原因是什么??

【问题讨论】:

    标签: iphone ios json asihttprequest foursquare


    【解决方案1】:

    发生这种情况是因为您在请求时没有正确传递 V(date) 值,您只是传递了在该日期之后可能无法工作的静态(旧)。 您必须在 URL 字符串中传递更新的(当前)V(date),即V 应该是您向 FourSquare 发出请求的 CurrentDate。

    尝试下面的代码并再次使用。

    更新:还有一件事你应该传递一些东西来搜索 因为在您的请求中,您没有传递任何搜索查询

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init] autorelease];
    [dateFormatter setDateFormat:@"YYYYMMdd"];
    NSString *dateString = [dateFormatter stringFromDate:currDate];
    //dateString it;s used for being UpTodate for API request
    NSString* rest=@"street"
    // To be Search                                 
    

    MakeRequest 如下和代码的其余部分将与您使用的相同。

    NSString *foursquareURLString = [NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?ll=%0.6f,%0.6f&client_id=%@&client_secret=%@&v=%@&query=%@", 
                                 latitude,
                                 longitude,
                                 FOURSQUARE_CLIENT_ID, 
                                 FOURSQUARE_CLIENT_SECRET,dateString,rest];
    //Convert the Special characters into escapes. 
    
     foursquareURLString =[foursquareURLString  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
     ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:foursquareURLString]];
     //Rest will be same
    

    For More Detail About the FourSquare API endpoints go Through This Link

    我希望它会给你正确的结果,而不是 Null !!!!!!!。

    【讨论】:

    • 我对昨天的日期 (20120103) 进行了硬编码,只是为了尝试获得有效的响应。正如我之前所说,如果我在 Web 浏览器中使用完全相同的 URL,但在我的应用程序中为 NULL,我会返回 JSON。用 [NSDate date] 格式化的当前日期替换硬编码日期仍然返回 NULL。
    • @jmstone 你使用了正确的日期格式`NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init] autorelease]; [dateFormatter setDateFormat:@"YYYYMMdd"]; NSString *dateString = [dateFormatter stringFromDate:currDate]'...?
    • @jmstone 检查你是否获得了 URL,我已经更新了上面的答案。因为 URL 字符串可能有一些特殊字符,这些特殊字符需要转换为转义符。当点击那个 URl 字符串(具有特殊Character),浏览器自动填充 Escapes 对应的特殊字符
    • 我已经确认过了。更重要的是,我已经从生成有效 JSON 的浏览器中复制了 URL,并将其直接粘贴到 URL 字符串中(替换格式化字符串),但仍然返回 NULL。我在想可能是 a) 请求必须通过某种 webView,或者 b) 取回数据并从该数据创建 UTF8String 不是正确的方法。
    • 昨晚试过了 :) 还是没有回来。我已经尝试过 NSString *jsonCheck = [[NSString alloc] initWithData:self.foursquareData encoding:NSUTF8StringEncoding]; SBJsonParser *parser = [[SBJsonParser alloc] init]; NSDictionary *dict = [解析器对象WithData:self.foursquareData];
    猜你喜欢
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 2023-03-08
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多