finalappfinalapp2

Using the Bands in Town API we’re making an app to know when our favorite band or musician is in town.

 


Retrofit Library – https://square.github.io/retrofit/
JSON to POJO – http://www.jsonschema2pojo.org/
Bands in Town API – https://app.swaggerhub.com/api/Bandsintown/PublicAPI/3.0.0

Complete Project Files – https://github.com/kenocvr/ConcertTracker


 

 

 

Bands in Town API

Familiarize yourself with the API. Click “Expand Operations”.  I used Beyonce as the artist in my example. app_id can be anything but ideally the name of your app.

apimodel

Let’s take a look at the JSON response. Highlight and copy the JSON response (Everything in the Response Body). The structure of the JSON array is the same for every artist. We can build a model to take these JSON elements and turn them into Java objects (POJO) we need for the app.

apijsonresponse

 

POJO

It will take a long time to wire up every json element in our model correctly by hand. Instead, go to this website to build our POJO (Plain Old Java Object) model.

http://www.jsonschema2pojo.org/

IMPORTANT: There are a couple options on the right side we need to configure for this to work. Make sure source type is set to JSON and Annotation style is Gson.

pojoschema

Paste the JSON response from earlier into this text field. Generate the POJO model. You can either copy/paste the POJO code or save the file.

pojomodelgen

Android Studio

Open a new Android Studio project. Create a new package and paste the generated POJO classes. I called the app ConcertTracker and the package is PojoBands.

sidelegend1

Internet Permissions

add the necessary permissions to AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>

 

Dependencies

While we’re at it, let’s add the dependencies to build.gradle(module app):

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.2.0'
    compile 'com.android.support:cardview-v7:23.2.0'
    compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
    compile 'com.squareup.okhttp:okhttp:2.4.0'
    compile 'com.google.code.gson:gson:1.7.2'
    compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'

    compile 'org.glassfish.main:javax.annotation:4.0-b33'
    compile 'com.squareup.picasso:picasso:2.5.2'

    compile 'com.github.bumptech.glide:glide:3.5.2'
    compile 'com.android.support:support-v4:23.0.0'
    compile 'com.github.florent37:materialtextfield:1.0.5'
}

You’ll notice we’re using glide and picasso as dependencies to handle image loading. I felt glide was the best option (smooth image scrolling) but by all means use picasso if you prefer a lighter library.

Layout

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity">

    <TextView
        android:id="@+id/errorTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:hint="No Artist or Events in Database"
        />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/card_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

 

activity_main_gridview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivityGridview"
    android:orientation="vertical"
    android:weightSum="1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">


        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:weightSum="1"
            android:layout_gravity="top|left"
            android:gravity="top">

            <com.github.florent37.materialtextfield.MaterialTextField
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                app:mtf_image="@drawable/ic_search_black_24dp"

                app:mtf_cardCollapsedHeight="4dp"

                app:mtf_animationDuration="400"
                app:mtf_labelColor="@android:color/holo_red_dark"
                app:mtf_openKeyboardOnFocus="true"
                android:layout_gravity="top">

                <EditText
                    android:layout_width="fill_parent"
                    android:layout_height="60dp"
                    android:id="@+id/searchText"
                    android:layout_gravity="center_horizontal"
                    android:inputType="textAutoComplete|text" />

            </com.github.florent37.materialtextfield.MaterialTextField>

 

card_row.xml

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    card_view:cardCornerRadius="5dp"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"

        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_name"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:textSize="18sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/Beyoncé"
            android:textStyle="bold"
            android:textColor="@android:color/black" />
        <TextView
            android:id="@+id/tv_version"
            android:hint="@string/Beyoncé"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tv_api_level"
            android:hint="@string/Beyoncé"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textColor="@color/colorAccent" />
        <TextView
            android:id="@+id/buyTicketsTxt"
            android:hint="Find Tickets"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

    </LinearLayout>
</android.support.v7.widget.CardView>

row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@color/colorPrimaryDark"
    android:textAlignment="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/img_android"
        android:adjustViewBounds="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="1dp" />
    <TextView
        android:id="@+id/tv_android"
        android:layout_gravity="center"
        android:textColor="#f6f4f4"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

Retrofit Interface

Create a new java class of type Interface.

public interface RestApi {
    
    @GET("/artists/{artist}/events/search.json?api_version=2.0&app_id=RUCKERLABS&location=use_geoip&radius=150")
    Call<List<PojoBand>> getJSON(@Path(value = "artist", encoded = true) String artist);
}

 

MainActivity

private RecyclerView recyclerView;
private List<PojoBand> data;
private DataAdapter adapter;
private String artist;
private int newString;
private String searchString;
String url = "http://api.bandsintown.com";

GSON deserialization

void getReport() {

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    RestApi service = retrofit.create(RestApi.class);

Format strings for url

try {

    String encodedurl = URLEncoder.encode(artist, "UTF-8")
            .replace("+", "%20")
            .replace("-", "%20");
    Log.d("TEST", encodedurl);
    Call<List<PojoBand>> call = service.getJSON(encodedurl);

call.enqueue(new Callback<List<PojoBand>>() {
        @Override
        public void onResponse(Response<List<PojoBand>> response, Retrofit retrofit) {

            try {
                List<PojoBand> artistData = response.body();
                data = new List<PojoBand>() {
                   
                   /* Auto-generate methods*/
                   
                };

                adapter = new DataAdapter(artistData);
                recyclerView.setAdapter(adapter);

            } catch (Exception e) {
                e.printStackTrace();
            }

        }


        @Override
        public void onFailure(Throwable t) {

        }
    });
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: