How to use Retrofit with Backendless

Hello,

I am trying to get an object list via retrofit but it is not working and I couldn’t find any examples code Retrofit + Backendless. So, I wonder if you can provide a simple example how to use retrofit with Backendless REST.

Regards,
Kasim

Kasim,

We do not have any retrofit experts. Please post a solution here once you figure it out.

Regards,
Mark

I found a solution to use Retrofit 2 with Backendless API. Here is a simple example.

Entity classes:

public class Music {


  private String ownerId;
  private String filename;
  private String publicUrl;
  
  // Getters & Setters
}

A class with a list of Music objects needed for server response:

public class MusicList {
  // Variable name matchs the array name in payload
  public List<Music> data;
}

Service interface:

public interface MyApiService {


  @Headers( { "application-id: MY-APPLICATION-ID",
                      "secret-key: MY-SECRET-KEY",
                      "application-type: REST" } )
  @GET("Music")
  Call<List<Music>> getMusicList();
}

Client code:



Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://api.backendless.com/v1/data/")
        .addConverterFactory(GsonConverterFactory.create())
        .build();
		
MyApiService service = retrofit.create(MyApiService.class);


Call<MusicList> call = musicService.getMusicList();
        call.enqueue(new Callback<MusicList>() {


            @Override
            public void onResponse(Call<MusicList> call, Response<MusicList> response) {
                List<Music> musicList = response.body().data;
				// Do something...
            }


            @Override
            public void onFailure(Call<MusicList> call, Throwable t) {
                Log.e("Main Activity", t.toString());
            }
        });
		
	// ...

Thanks, Kasim!

great, thanks!