Codementor Events

Developing Android Client app with Django rest Framework

Published Aug 07, 2017Last updated Nov 16, 2017
Developing Android Client app with Django rest Framework

I will really appreciate if you support the project by clicking the star button on Github repository. I am going to publish new version soon https://github.com/hassanabidpk/searchrestaurant


Last year, I wrote a blog post about developing an iOS app with django backend. Many people requested an android version of it. So I decided to write this blog post. Although, I have already implemented the basic android version, however I couldn’t write the blog post until now.

Same with iOS version, I will use examples from searchrestaurant app.

The API end points are as below:

Common Endpoints
  • Get list of all restaurants: /api/v1/
  • Get list of restaurants for particular location and type: /api/v1/ with params location and rtype
  • Sample Request: https://searchrestaurant.pythonanywhere.com/api/v1/?format=json&location=oslo&rtype=pizza
Response Values
  • In case of error you will get an error key with a status code
  • You will get an array of dicts eaching containing information about place(restaurant,coffee shop etc.)
Name Type Definition
name string Name of the restaurant
latitude string (convert to double) Latitude of the restaurant location
longitude string (convert to double) Longitude of the restaurant location
address string Local address of the restaurant
checkins number number of checkins at this restaurant
photo_url string A 300x200 image of the restaurant
phone_number string Phone number of the restaurant if available otherwise N/A
created_at string (convert to datetime) A datetime string when this object was created
updated_at string (convert to datetime) A datetime string when this object was updated

You can read more about Django rest framework code in previous blog post. Source code is available here.

From here on, I will explain Java Code to retrieve the restaurant list and show in the app. I used retrofit, gson and glide libraries for this project.

We make our API interface class as follows [source]

package co.searchrestaurant.android.app.fetch;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

/**
 * Created by hassanabid on 2/27/16.
 */
public interface SearchRestaurantApi {

    @GET("api/v1/")
    Call<SearchRestaurantResponse[]> getRestaurantsList(@Query("format") String format, @Query("location") String location,
                                               @Query("rtype") String rtype);


}

In the similar way we make our response object as follows [Source]

package co.searchrestaurant.android.app.fetch;

import java.util.List;

/**
 * Created by hassanabid on 2/27/16.
 */
public class SearchRestaurantResponse {

    public String status;
    public String name;
    public  String address;
    public int checkins;
    public String latitude;
    public String longitude;
    public String photo_url;
    public String venue_id;
    public String phone_number;
    public String created_at;
    public String updated_at;


}

Next up is to use the API and fetch data from network. Refer to RestaurantListActivity.java We use Retrofit for all network calls. Once we receive data from the network we populate recycle view.

private void initiateRestaurantApi(String place, String query,final View recyclerView) {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(API_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        SearchRestaurantApi api = retrofit.create(SearchRestaurantApi.class);
        Call<SearchRestaurantResponse[]> call = api.getRestaurantsList("json",place,query);
        progessBar.setVisibility(View.VISIBLE);
        call.enqueue(new Callback<SearchRestaurantResponse[]>() {
            @Override
            public void onResponse(Response<SearchRestaurantResponse[]> response) {
                if(response.isSuccess()) {
                    Log.d(LOG_TAG, "success - response is " + response.body());
                    restaurants = Arrays.asList(response.body());
                    setupRecyclerView((RecyclerView) recyclerView);
                    progessBar.setVisibility(View.GONE);

                } else {
                    progessBar.setVisibility(View.GONE);
                    Log.d(LOG_TAG, "failure response is " + response.raw().toString());

                }
            }

            @Override
            public void onFailure(Throwable t) {
                Log.d(LOG_TAG, " Error :  " + t.getMessage());
            }
        });

    }

Source code for the app is on Github [link]

There are certain improvements that needs to be done.

  1. Use Realm for offline data storage
  2. Improve the UI

I have written a tutorial for developing an android app with Retrofit and Realm. You can see the guide here

You can read previous blog post about iOS here

Discover and read more posts from Hassan Abid
get started
post comments2Replies
nimmi Alexander
6 years ago

hi

Sahar Gull
7 years ago

would you like to get more downloads for your apps or games in free. add your app on this site https://reviewlancer.com/