Yelp API issues

Newbie programmer so please forgive my lack of knowledge or process.

I was trying to integrate this into my app: https://github.com/Yelp/yelp-api/tree/master/v2/csharp
I wanted to start by just trying to add the code from the Program.cs in the git directory into the MainActivity.cs in Xamarin.
When trying to build, it gives me several errors that I have not been able to resolve:

At using CommandLine and using SimpleOAuth it says: Error CS0246: The type or namespace name 'CommandLine (SimpleOAuth)' could not be found (are you missing a using directive or an assembly reference?) (CS0246) (yelp_android)
I figured this was because of missing packages so I tried to install the packages that the git repository had which are:
-CommandLineParser.1.9.71
-Newtonsoft.Json.6.0.3
-SimpleOAuth.Net.1.0.1
The Newstonsoft and the SimpleOAuth packages seem to add just fine(though I am still getting the error at using SimpleOAuth),but when trying to add the Command Line Parser Library I get the following error:
Could not install package 'CommandLineParser 1.9.71'. You are trying to install this package into a project that targets 'MonoAndroid,Version=v4.4.87', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
On top of all that I also get 2 more errors at lines 189 and 196 I get 2 errors each, both identical:
Error CS0246: The type or namespace name 'Option' could not be found (are you missing a using directive or an assembly reference?) (CS0246) (yelp_android)
Error CS0246: The type or namespace name 'OptionAttribute' could not be found (are you missing a using directive or an assembly reference?) (CS0246) (yelp_android)
I right clicked on the position where the error is occurring and went to Resolve> using Android.Media;, but that only fixed the 'Option' error.

Please, any help is greatly appreciated! I feel like I've tried everything and yet I'm still missing something.
Here is my build information if it's useful:
=== Xamarin Studio ===

    Version 5.5.4 (build 15)
    Installation UUID: ca7dde21-b8d3-4f4c-8630-c26191c1af99
    Runtime:
        Microsoft .NET 4.0.30319.18408
        GTK+ 2.24.22 (MS-Windows theme)
        GTK# 2.12.26

    === Xamarin.Android ===

    Version: 4.20.0 (Starter Edition)
    Android SDK: C:\Users\Chris\AppData\Local\Android\android-sdk
        Supported Android versions:
            1.6    (API level 4)
            2.1    (API level 7)
            2.2    (API level 8)
            2.3    (API level 10)
            3.0    (API level 11)
            3.1    (API level 12)
            3.2    (API level 13)
            4.0    (API level 14)
            4.0.3  (API level 15)
            4.1    (API level 16)
            4.2    (API level 17)
            4.3    (API level 18)
            4.4    (API level 19)
            4.4.87 (API level 20)
    Java SDK: C:\Program Files (x86)\Java\jdk1.6.0_39
    java version "1.6.0_39"
    Java(TM) SE Runtime Environment (build 1.6.0_39-b04)
    Java HotSpot(TM) Client VM (build 20.14-b01, mixed mode, sharing)

    === Build Information ===

    Release ID: 505040015
    Git revision: f93940a35458a18052f1a25e106e62ca970d9c40
    Build date: 2014-11-19 13:26:21-05
    Xamarin addins: dc23cbd91a3a0e1d326328e1229e86c942a49ec8

    === Operating System ===

    Windows 6.1.7601.65536 (64-bit)

Here is the code for MainActivity.cs:
using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using CommandLine;
using Newtonsoft.Json.Linq;
using SimpleOAuth;

namespace yelp_android
{
    [Activity (Label = "yelp_android", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        int count = 1;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button> (Resource.Id.myButton);

            button.Click += delegate {
                button.Text = string.Format ("{0} clicks!", count++);
            };
        }
    }
}


/// <summary>
/// Yelp API v2.0 code sample.
/// This program demonstrates the capability of the Yelp API version 2.0
/// by using the Search API to query for businesses by a search term and location,
/// and the Business API to query additional information about the top result
/// from the search query.
///
/// Please refer to http://www.yelp.com/developers/documentation for the API documentation.
///
/// Sample usage of the program:
/// `YelpAPI.exe --term="bars" --location="San Francisco, CA"`
/// </summary>
namespace YelpAPI
{
    /// <summary>
    /// Class that encapsulates the logic for querying the API.
    ///
    /// Users have to set the OAuth credentials properties
    /// in order to use this class.
    /// </summary>
    class YelpAPIClient
    {
        /// <summary>
        /// Consumer key used for OAuth authentication.
        /// This must be set by the user.
        /// </summary>
        private const string CONSUMER_KEY = null;

        /// <summary>
        /// Consumer secret used for OAuth authentication.
        /// This must be set by the user.
        /// </summary>
        private const string CONSUMER_SECRET = null;

        /// <summary>
        /// Token used for OAuth authentication.
        /// This must be set by the user.
        /// </summary>
        private const string TOKEN = null;

        /// <summary>
        /// Token secret used for OAuth authentication.
        /// This must be set by the user.
        /// </summary>
        private const string TOKEN_SECRET = null;

        /// <summary>
        /// Host of the API.
        /// </summary>
        private const string API_HOST = "http://api.yelp.com";

        /// <summary>
        /// Relative path for the Search API.
        /// </summary>
        private const string SEARCH_PATH = "/v2/search/";

        /// <summary>
        /// Relative path for the Business API.
        /// </summary>
        private const string BUSINESS_PATH = "/v2/business/";

        /// <summary>
        /// Search limit that dictates the number of businesses returned.
        /// </summary>
        private const int SEARCH_LIMIT = 3;

        /// <summary>
        /// Prepares OAuth authentication and sends the request to the API.
        /// </summary>
        /// <param name="baseURL">The base URL of the API.</param>
        /// <param name="queryParams">The set of query parameters.</param>
        /// <returns>The JSON response from the API.</returns>
        /// <exception>Throws WebException if there is an error from the HTTP request.</exception>
        private JObject PerformRequest(string baseURL, Dictionary<string, string> queryParams=null)
        {
            var query = System.Web.HttpUtility.ParseQueryString(String.Empty);

            if (queryParams == null)
            {
                queryParams = new Dictionary<string, string>();
            }

            foreach (var queryParam in queryParams)
            {
                query[queryParam.Key] = SimpleOAuth.Utilities.UrlHelper.Encode(queryParam.Value);
            }

            var uriBuilder = new UriBuilder(baseURL);
            uriBuilder.Query = query.ToString();

            var request = WebRequest.Create(uriBuilder.ToString());
            request.Method = "GET";

            request.SignRequest(
                new Tokens {
                    ConsumerKey = CONSUMER_KEY,
                    ConsumerSecret = CONSUMER_SECRET,
                    AccessToken = TOKEN,
                    AccessTokenSecret = TOKEN_SECRET
                }
            ).WithEncryption(EncryptionMethod.HMACSHA1).InHeader();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            var stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            return JObject.Parse(stream.ReadToEnd());
        }

        /// <summary>
        /// Query the Search API by a search term and location.
        /// </summary>
        /// <param name="term">The search term passed to the API.</param>
        /// <param name="location">The search location passed to the API.</param>
        /// <returns>The JSON response from the API.</returns>
        public JObject Search(string term, string location)
        {
            string baseURL = API_HOST + SEARCH_PATH;
            var queryParams = new Dictionary<string, string>()
            {
                { "term", term },
                { "location", location },
                { "limit", SEARCH_LIMIT.ToString() }
            };
            return PerformRequest(baseURL, queryParams);
        }

        /// <summary>
        /// Query the Business API by a business ID.
        /// </summary>
        /// <param name="business_id">The ID of the business to query.</param>
        /// <returns>The JSON response from the API.</returns>
        public JObject GetBusiness(string business_id)
        {
            string baseURL = API_HOST + BUSINESS_PATH + business_id;
            return PerformRequest(baseURL);
        }
    }

    /// <summary>
    /// Command-line options abstraction.
    /// </summary>
    class Options
    {
        /// <summary>
        /// Gets and sets the Term property.
        /// </summary>
        /// <value>The search term specified by the user.</value>
        [Option('t', "term", DefaultValue="dinner", HelpText = "Search term")]
        public string Term { get; set; }

        /// <summary>
        /// Gets and sets the Location property.
        /// </summary>
        /// <value>The location term specified by the user.</value>
        [Option('l', "location", DefaultValue="San Francisco, CA", HelpText = "Search Location")]
        public string Location { get; set; }
    }

    /// <summary>
    /// Class that encapsulates the program entry point.
    /// </summary>
    class Program
    {
        /// <summary>
        /// Queries the API by the input values from the user, and prints
        /// the result on the console.
        /// </summary>
        /// <param name="term">The search term to query.</param>
        /// <param name="location">The location of the business to query.</param>
        public static void QueryAPIAndPrintResult(string term, string location)
        {
            var client = new YelpAPIClient();

            Console.WriteLine("Querying for {0} in {1}...", term, location);

            JObject response = client.Search(term, location);

            JArray businesses = (JArray)response.GetValue("businesses");

            if (businesses.Count == 0)
            {
                Console.WriteLine("No businesses for {0} in {1} found.", term, location);
                return;
            }

            string business_id = (string)businesses[0]["id"];

            Console.WriteLine(
                "{0} businesses found, querying business info for the top result \"{1}\"...",
                businesses.Count,
                business_id
            );

            response = client.GetBusiness(business_id);

            Console.WriteLine(String.Format("Result for business {0} found:", business_id));        
            Console.WriteLine(response.ToString());
        }

        /// <summary>
        /// Program entry point.
        /// </summary>
        /// <param name="args">The command-line arguments.</param>
        static void Main(string[] args)
        {
            var options = new Options();

            try
            {
                CommandLine.Parser.Default.ParseArguments(args, options);
            }
            catch (CommandLine.ParserException)
            {
                Console.Error.WriteLine("Failed to parse command line options.");
                Environment.Exit(-1);
            }

            try
            {
                Program.QueryAPIAndPrintResult(options.Term, options.Location);
            }
            catch (WebException error)
            {
                Console.Error.WriteLine("HTTP request failed, got {0}, abort program.", error.Message);
                Environment.Exit(-1);
            }
        }
    }
}

Posts

  • SebastianSeidel.9226SebastianSeidel.9226 DEInsider, University ✭✭✭✭

    Not having looked at your code I would determine that the CommandLineParser package is not compatible with Xamarin.Android and thus it won't get installed.

  • vamusevamuse USMember

    Yeah that's kind of what I figured. Is that what's causing my other errors? And can I still get the yelp API working without the CommandLineParser? THANKS FOR THE RESPONSE!

  • NicoleKleinNicoleKlein USMember

    vamuse, did you ever figure it out? i am trying to do the same thing in an iOS project and would love some assistance.

  • ADCADC CRMember

    They can help me, when I try to compile I get the following error ... I've looked everywhere, and install Framework.NET PORTABLE that was one of the solutions I found but did not work.

  • SebastianSeidel.9226SebastianSeidel.9226 DEInsider, University ✭✭✭✭

    There is a typo. It must be ContentPage not ContenPage. See the missing t.

  • ADCADC CRMember

    These are some warnings displayed and error :neutral:

    C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(5,5): Warning MSB3644: The reference assemblies for framework ".NETFramework,Version=v4.5,Profile=Profile78" were not found. To resolve this, install the SDK or Targeting Pack for this framework version or retarget your application to a version of the framework for which you have the SDK or Targeting Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend. (MSB3644) (Interfaz)

    C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(5,5): Warning MSB3644: The reference assemblies for framework ".NETFramework,Version=v4.5" were not found. To resolve this, install the SDK or Targeting Pack for this framework version or retarget your application to a version of the framework for which you have the SDK or Targeting Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend. (MSB3644) (Interfaz)

    C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(5,5): Warning MSB3270: There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll", "x86". This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project. (MSB3270) (Interfaz)

    C:\Users\adcmovil\Documents\Projects\Interfaz\Interfaz\contenedor1.cs(29,29): Error CS0246: The type or namespace name 'ContenPage' could not be found (are you missing a using directive or an assembly reference?) (CS0246) (Interfaz)

Sign In or Register to comment.