Code Monkey home page Code Monkey logo

yelp-fusion-android's Introduction

yelp-fusion-android

An Android library for the Yelp Fusion API v3. It simplifies the process of authentication, request construction, and response parsing for Android developers using the Yelp API v3. This clientlib has been tested with applications written in Android API level 15 and 25. This library inspired by YelpAndroid

Installation

Download

<dependency>
  <groupId>io.github.ranga543</groupId>
  <artifactId>yelp-fusion-client</artifactId>
  <version>0.1.5</version>
  <type>pom</type>
</dependency>

or Gradle:

repositories {
    jcenter()
}

dependencies {
    ...
    compile 'io.github.ranga543:yelp-fusion-client:0.1.5'
    ...
}

Proguard rules for the library can be found here

Usage

Basic usage

This library uses a YelpFusionApi object to query against the API. Instantiate a YelpFusionApi object by using YelpFusionApiFactory with your API key.

YelpFusionApiFactory apiFactory = new YelpFusionApiFactory();
YelpFusionApi yelpFusionApi = apiFactory.createAPI(apiKey);

Once you have a YelpFusionApi object you can use the getBusinessSearch function to generate a Call object which makes a request to the Search API.

The general params and locale options should be passed to the method as a Map<String, String>. The full list of parameters can be found in the Search API Documentation.

Map<String, String> params = new HashMap<>();

// general params
params.put("term", "indian food");
params.put("latitude", "40.581140");
params.put("longitude", "-111.914184");

Call<SearchResponse> call = yelpFusionApi.getBusinessSearch(params);

Now you can execute the Call object to send the request.

Response<SearchResponse> response = call.execute();

You can also pass in a Callback object to send the request asynchronously. For more see Asynchronous Requests section.

Callback<SearchResponse> callback = new Callback<SearchResponse>() {
    @Override
    public void onResponse(Call<SearchResponse> call, Response<SearchResponse> response) {
        SearchResponse searchResponse = response.body();
        // Update UI text with the searchResponse.
    }
    @Override
    public void onFailure(Call<SearchResponse> call, Throwable t) {
        // HTTP error happened, do something to handle it.
    }
};

call.enqueue(callback);

Additionally there are two more search methods for searching by a Phone or for Transaction:

// Phone Search
Call<SearchResponse> call = yelpFusionApi.getPhoneSearch("+18014384823");
Response<SearchResponse> response = call.execute();

// Transaction Search
Call<SearchResponse> call = yelpFusionApi.getTransactionSearch("delivery", params);
Response<SearchResponse> response = call.execute();

To query the Business API, use the getBusiness function with a id.

Call<Business> call = yelpFusionApi.getBusiness("saffron-valley-south-jordan");
Response<Business> response = call.execute();

Asynchronous Requests

This library uses Retrofit as the HTTP client. To send a request asynchronously, use Call.enqueue() to set Callback function for an asynchronous request.

Callback<Business> callback = new Callback<Business>() {
    @Override
    public void onResponse(Call<Business> call, Response<Business> response) {
        Business business = response.body();
        // Update UI text with the Business object.
    }
    @Override
    public void onFailure(Call<Business> call, Throwable t) {
        // HTTP error happened, do something to handle it.
    }
};

Call<Business> call = yelpFusionApi.getBusiness(id);
call.enqueue(callback);

You can cancel asynchronous requests by simply call cancel() on Call objects. It is important to cancel your calls while your Activity is being destroyed to avoid memory leaks.

Call<Business> call = yelpFusionApi.getBusiness(businessId);
call.enqueue(callback);

// Activity is being destroyed and the call should be canceled.
call.cancel();

For more information about the usage of asynchronous requests in Retrofit, see Retrofit documentation.

Responses

After Call object is executed, a Response contains parsed Java objects will be returned, use Response.body() to get parsed Java objects.

Search and phone search responses are parsed into SearchResponse objects.

Call<SearchResponse> call = yelpFusionApi.getBusinessSearch(params);
SearchResponse searchResponse = call.execute().body();

int totalNumberOfResult = searchResponse.getTotal();  // 3

ArrayList<Business> businesses = searchResponse.getBusinesses();
String businessName = businesses.get(0).getName();  // "JapaCurry Truck"
Double rating = businesses.get(0).getRating();  // 4.0

Business responses are parsed into Business objects directly.

Call<Business> call = yelpFusionApi.getBusiness("japacurry-truck-san-francisco");
Response<Business> response = call.execute();
Business business = response.body();

String businessName = business.getName();  // "JapaCurry Truck"
Double rating = business.getRating();  // 4.0

For a full list of available response fields, take a look at the documentation or the classes defined in com.yelp.fusion.client.models.

Contributing

  1. Fork it (https://github.com/ranga543/yelp-fusion-android/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Testing

Please write tests for any new features. We use JUnit + Gradle so just run ./gradlew test to run the full test suite. To know more about running JUnit tests in Gradle, see [Gradle: The Java Plugin - Test](https://docs.gradle .org/current/userguide/java_plugin.html#sec:java_test).

If you are adding a new integration test, you will need to connect to the Yelp Fusion API. You can set this up by putting your API keys into src/test/resources/credentials.yaml in the following format:

api_key: YOUR_API_KEY

To run the integration tests, execute ./gradlew integrationTest. Integration tests will not be ran in the build process by executing ./gradlew build.

yelp-fusion-android's People

Contributors

ranga543 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

yelp-fusion-android's Issues

Category is not serializable

When trying to pass a list of businesses from one activity to another, it fails with Caused by: java.io.NotSerializableException: com.yelp.fusion.client.models.Category

It appears as though Category is not serializable. Can changes be made to fix this?

Cant seem to get the API keys to work.

public class selectionPage extends AppCompatActivity {
TextView mRestaurantTitle, mRateCat;
ImageView mMainImage;
YelpFusionApiFactory mApiFactory;
YelpFusionApi mYelpFusionApi;
Map<String, String> mParams;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.selection_page);

    mRestaurantTitle = (TextView) findViewById(R.id.titleLabel);
    mRateCat = (TextView) findViewById(R.id.rateCatLabel);
    mMainImage = (ImageView) findViewById(R.id.mainImage);

    Picasso
            .with(this)
            .load("http://i.imgur.com/DvpvklR.png")
            .into(mMainImage);

    mMainImage.setOnTouchListener(new OnSwipeTouchListener(this){
        public void onSwipeTop() {
            Toast.makeText(selectionPage.this, "top", Toast.LENGTH_SHORT).show();
        }
        public void onSwipeRight() {
            Toast.makeText(selectionPage.this, "right", Toast.LENGTH_SHORT).show();
        }
        public void onSwipeLeft() {
            Toast.makeText(selectionPage.this, "left", Toast.LENGTH_SHORT).show();
        }
        public void onSwipeBottom() {
            Toast.makeText(selectionPage.this, "bottom", Toast.LENGTH_SHORT).show();
        }
    });


    mApiFactory = new YelpFusionApiFactory();
    try {
        mYelpFusionApi = mApiFactory.createAPI(
                getResources().getString(R.string.appId),
                getResources().getString(R.string.appSecret)
        );
    } catch (IOException e) {
        e.printStackTrace();
    }

    mParams = new HashMap<>();
    mParams.put("term", "indian food");

    new FetchPictures().execute();
}

class FetchPictures extends AsyncTask<String, String, String> {

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected String doInBackground(String... params) {

        Call<SearchResponse> call = mYelpFusionApi.getBusinessSearch(mParams);

        Response<SearchResponse> response = null;

        try {
            response = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if(response != null) {
            Log.v("Business", response.body().getBusinesses().toString());
        }
      //  call.enqueue(callback);
        return null;
    }
}

}

Yelp Fusion (Android Studio) help!

NOTE: the instructions seem pretty straightforward so I'm actually super confuse why I'm having so much trouble....
So I’ve been running into a number of problems all of which force my app to crash the first being when i make a call to the YelpFusionApiFactory’s createAPI function passing the appID and the AppSecret. I was able to get around this by going to a third party (i.e. Rapid) which generated the access token. i know use the …Factory’s createAPI function passing in the access token. however this does not fix the original problem i was having the next few problems all occur when trying to use the actual api for instance calling execute on the response object....i am a newbie to yelp and android studio...though i feel i have come accustom to android studio .....THANK YOU IN ADVANCE FOR ANY HELP
the app itself doesn't do anything.. at the moment I'm just trying to get comfortable with api

screen shot 2017-05-09 at 9 02 55 am

Example code is erroneous?

               
 Map<String, String> params = new HashMap<>();
                params.put("term", "Boba");
                params.put("latitude", "37.86701009652528");
                params.put("longitude", "-122.2539898316383");

                Call<SearchResponse> call = yelpFusionApi.getBusinessSearch(params);

This is my SearchResponse call to get params, but get java.lang.NullPointerException: Attempt to invoke interface method 'retrofit2.Call com.yelp.fusion.client.connection.YelpFusionApi.getBusinessSearch(java.util.Map)' on a null object reference

This is after I long click on my android screen, to invoke a business search (Trying to make a basic yelp app in android).

In the examples it shows two calls, Call call = yelpFusionApi.getBusinessSearch(params); and Call call = yelpFusionApi.getBusinessSearch("San Francisco", params);

getBusinessSearch only takes in Map<String, String> params, so why is there an extra string in the arguments?

How to get more than 50 businesses?

I don't know how to get more than 50 results for a business search made.

For some reason, the maximum value for limit can be set to only 50. The getTotal method gives me 328 results when I set the limit to 50.

This only lets me get 50 results out of 328. How do I get the rest 278 results?

Thanks.

Missing bintray.properties file?

When I try to compile this, I get an error about:
../yelp-fusion-android-master/bintray.properties (No such file or directory)

Should I make my own such file? If so, how do I know what it looks like?
I note this file is in your gitignore, but it seems like it's needed for the code below in your gradle file:

Properties bintrayProperties = new Properties()
bintrayProperties.load(project.rootProject.file('bintray.properties').newDataInputStream())

bintray {
user = bintrayProperties.getProperty('user')
key = bintrayProperties.getProperty('key')
configurations = ['archives']
pkg {
repo = 'android'
name = 'yelp-fusion-client'
userOrg = 'ranga543'
licenses = ['MIT']
vcsUrl = 'https://github.com/ranga543/yelp-fusion-android.git'
publish = true
githubRepo = 'ranga543/yelp-fusion-android'
githubReleaseNotesFile = 'README.md'
version {
name = '0.1.5'
desc = 'Yelp Fusion Client 0.1.5'
released = new Date()
vcsTag = 'v0.1.5'
gpg {
sign = true
passphrase = bintrayProperties.getProperty('gpg.password')
}
}
}
}

Dependency resolution error

Building a project with Maven dependency:

<dependency> 
<groupId>io.github.ranga543</groupId> 
<artifactId>yelp-fusion-client</artifactId>
<version>0.1.3</version>
</dependency>

results in the following Maven error:

The following artifacts could not be resolved: io.github.ranga543:yelp-fusion-client:jar:0.1.2, com.android.support:appcompat-v7:jar:25.1.1: Failure to find io.github.ranga543:yelp-fusion-client:jar:0.1.2 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced

Dependency:

<dependency>
<groupId>io.github.ranga543</groupId>
<artifactId>yelp-fusion-client</artifactId>
<version>0.1.3</version>
<type>pom</type>
</dependency>

results in:

Failure to find com.android.support:appcompat-v7:jar:25.1.1 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced

Version 0.1.3 and 0.1.2 both result in the same errors.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.