如何通过API从iOS应用程序发出http请求

问题描述 投票:0回答:1

我正在使用 Xcode 制作一个应用程序,允许用户搜索数据库。这是 goodzer.com 数据库,我想知道如何将其 API 放入 iOS 应用程序内的 http 请求中。请有任何帮助。

ios database httprequest
1个回答
4
投票

使用RestKitAFNetworking等框架。它们都非常易于使用,可让您立即连接到 RESTful API。

您需要通过他们的网站注册 API 密钥。

搜索他们的数据库看起来像这样(使用 AFNetworking):

NSString url = @"http://api.goodzer.com/products/v0.1/search_in_store/?storeId=fAfVfDCd&query=ipad+case&priceRange=20:50&apiKey=<Your_API_key>";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

这将返回类似以下内容:

{
    "status": "ok",
    "realtime_availability": true,
    "products_found": 199,
    "products": [
        {
            "id": 1573967503,
            "title": "Harley-Davidson - Envelope Case for Apple iPad, iPad 2 and iPad (3rd Genera ion) - Black",
            "price": 34.99,
            "url": "http://www.bestbuy.com/site/Harley-Davidson+-+Envelope+Case+for+Apple%AE+iPad%AE%2C+iPad+2+and+iPad+(3rd+Generation)+-+Black/5422949.p?id=1218643212507&skuId=5422949&cmp=RMX&ky=2nOYnsgAdnUUUygixM6IZsxTJF3fsquqM",
            "image": "http://img.goodzer.com/peregimator/?size=medium&valign=center&sign=d4e70e4e&image=http%3A//images.bestbuy.com/BestBuy_US/images/products/5422/5422949_sc.jpg"
        },
        {
            "id": 1573970162,
            "title": "Samsonite - Shuttle Case for Apple iPad, iPad 2 and iPad (3rd Generation) - Black/White",
            "price": 39.99,
            "url": "http://www.bestbuy.com/site/Samsonite+-+Shuttle+Case+for+Apple%AE+iPad%AE%2C+iPad+2+and+iPad+(3rd+Generation)+-+Black/White/5763475.p?id=1218692161151&skuId=5763475&cmp=RMX&ky=2nOYnsgAdnUUUygixM6IZsxTJF3fsquqM",
            "image": "http://img.goodzer.com/peregimator/?size=medium&valign=center&sign=9ac997a0&image=http%3A//images.bestbuy.com/BestBuy_US/images/products/5763/5763475_sc.jpg"
        },
    ...
    ]
}
© www.soinside.com 2019 - 2024. All rights reserved.