VerachadW/restful-android-kotlin — pull request #4 by kittinunv-taskworld (2014-10-27 04:53:03+00:00)
Title: Feature/robospice retrofit

This is a result of our new and shiny robospice+retrofit+kotlin.

First, define model. (In Java, as Realm required)

```Movie.java```
```Java
public class Movie {

private int id;
private String title;
private float popularityScore;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public float getPopularityScore() {
return popularityScore;
}

public void setPopularityScore(float popularityScore) {
this.popularityScore = popularityScore;
}

public static class ResultList {

private List results;

public List getResults() {
return results;
}
}
}
```

Create API interface (in trait by using Kotlin)
```TheMovieDBAPI.kt```

``` Kotlin
trait TheMovieDBAPI {
GET("/discover/movie")
fun discoverMovies(Query("sort_by") sortBy: String): Movie.ResultList
}
```

Express your request with request object inherited from ```RetrofitSpiceRequest```

```DiscoverMovieSpiceRequest.kt```

``` Kotlin
class DiscoverMovieSpiceRequest(val sortBy: String) :
RetrofitSpiceRequest(javaClass(), javaClass()) {

override fun loadDataFromNetwork(): Movie.ResultList? {
return getService().discoverMovies(sortBy)
}

}
```

Then, make use of it like usual in controller class

``` Kotlin
btFetchMovie.setOnClickListener { view ->
getServiceManager().execute(DiscoverMovieSpiceRequest("popularity.desc"), EventBusResponseListener())
getServiceManager().execute(DiscoverMovieSpiceRequest("vote_average.desc"), object: RequestListener {
override fun onRequestFailure(spiceException: SpiceException?) {
toast(spiceException?.getMessage())
}

override fun onRequestSuccess(results: Movie.ResultList?) {
Log.i(tag(), results?.getResults()?.size.toString())
}
})

}
```