Skip to content

Conversations Tutorial

Sergio Azua edited this page Jan 19, 2016 · 17 revisions

In order to work with the Conversations SDK, please make sure you have met the minimum requirements defined on the Home Page.

For this tutorial, we'll create a simple application using Android Studio 1.5.1

Contents

  1. Create the Project
  2. Initialize the BVSDK
  3. Construct the API Request

Create the Project

  1. From the Android Studio welcome screen click Start a new Android Studio project

    Provide an Application name and then hit Next taking all the defaults until you are able to press Finish.

App Gradle - Add dependencies

  1. Open Project view

  2. Filter on Android files - you may need to right click on toolbar and deselect Show Views as Tabs to get this perspective

  3. Open app module's build.gradle

  4. Include Maven Central repository and add Conversations module to app gradle dependencies

    dependencies {
        compile 'com.bazaarvoice.bvandroidsdk:conversations:3.0.0'
    } 
    repositories {
        mavenCentral()
    }
  5. Android studio should show a suggestion toward the top of the editor asking you to Sync Now. Press the Sync Now button.

Initialize the BVSDK

Now we are ready to set up our API and client ID and initialize the BVSDK.
  1. Create a new Java Class in your main app package by right clicking the package -> New -> Java Class and name it BVApplication. You will now make it a subclass of android.app.Application, override onCreate and initialize the SDK. The resulting code should look as follows although you will need to provide the client id, environment, Conversations key, and log level:

TIP: If you do not have a key yet and just want to get familiar with the SDK you can use a conversations key value of kuy3zj9pr3n7i0wxajrzj04xo, a clientId of apitestcustomer, and BazaarEnvironment.STAGING environment.

```java
package com.example.bazaarvoice.conversationstutorial;

import android.app.Application;

import com.bazaarvoice.bvandroidsdk.BVLogLevel;
import com.bazaarvoice.bvandroidsdk.BVSDK;
import com.bazaarvoice.bvandroidsdk.BazaarEnvironment;

/**
 * Created by Bazaarvoice on 1/18/16.
*/
public class BVApplication  extends Application {

    @Override
    public void onCreate(){
        super.onCreate();

        // Builder used to initialize the Bazaarvoice SDKs
        BVSDK bvsdk = new BVSDK.Builder(this)
                .clientId(YOUR_CLIENT_ID)
                .shopperAdvertisingEnvironment(BazaarEnvironment.PRODUCTION)
                .apiKeyConversations(YOUR_CONVERSATIONS_KEY)
                .logLevel(BVLogLevel.INFO)
                .build();
    }
}
```
  1. Open AndroidManifest.xml and set the name of the class for the application entry and request internet permission

    <uses-permission android:name="android.permission.INTERNET" />
    
        <application
            android:name=".BVApplication">
            ...
  2. Build and run your app to make sure it is error free and loads "Hello World!".

Construct the API Request

At this point you should have the BVSDK installed and initialized, so we can now make an API call to the Conversations API.

  1. Open up MainActivity.java

  2. Make an API call.

    In the onCreate method make an API call to get some reviews and use an OnBazaarResponse callback to output the result to the console

    BazaarRequest request = new BazaarRequest();
    request.sendDisplayRequest(RequestType.REVIEWS, null, new OnBazaarResponse() {
        @Override
        public void onResponse(String url, JSONObject response) {
            try {
                Log.d("MainActivity", response.toString(1));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void onException(String message, Throwable exception) {
            //handle exception
        }
    });
  3. Finally, run the application and check the console for a successful API call.

    In order the get more familiar with the Conversations module APIs, you should become familiar with the BazaarRequest and BazaarParams classes and subclasses which are used to construct all the Conversations API calls. Review Examples in the ads package within the main app package com.example.bazaarvoice.bv_android_sdk_3 of the project.

Clone this wiki locally