by Nick Fedesna
Google only has eyes for AOSP:
HttpURLConnection with
AsyncTask
Udacity courses teach pure Java w/ AsyncTask
but does mention it's not really the best way.
public interface GithubService {
@GET("users/{user}/repos")
Call<List<Repo>> listRepos(@Path("user") String user);
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com")
.build();
GitHubService service = retrofit.create(GitHubService.class)
Call<List<Repo>> reposCall = service.listRepos("octocat");
reposCall.enqueue(new Callback<List<Repo>>() { … });
Response<List<Repository>> reposResponse = reposCall.execute();
@GET("users/list?sort=desc")
Call<List<User>> userListSorted();
@GET("users/list")
Call<List<User>> usersList(@Query("sort") String sort);
@GET("group/{id}/users")
Call<List<User>> groupList(@Path("id") int groupId);
@GET("group/{id}/users")
Call<List<User>> groupList(@Path("id") int groupId,
@QueryMap Map<String, String> options);
@POST("users/new")
Call<User> createUser(@Body User user);
@FormUrlEncoded
@POST("user/edit")
Call<User> updateUser(@Field("first_name") String first,
@Field("last_name") String last);
@Multipart
@PUT("user/photo")
Call<User> updateUser(@Part("photo") RequestBody photo,
@Part("description") RequestBody description);
@Headers("Cache-Control: max-age=640000")
@GET("widget/list") Call<List<Widget>> widgetList();
@Headers({
"Accept: application/vnd.github.v3.full+json",
"User-Agent: Retrofit-Sample-App"
})
@GET("users/{username}")
Call<User> getUser(@Path("username") String username);
@GET("user")
Call<User> getUser(@Header("Authorization") String authorization)
Retrofit retro = new Retrofit.Builder()
.baseUrl("https://api.github.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
Retrofit retro = new Retrofit.Builder()
.baseUrl("https://api.github.com")
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
public interface GithubService {
@GET("users/{user}/repos")
Observable<List<Repo>> listRepos(@Path("user") String user);
}
api.listRepos("octocat")
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.immediate())
.subscribe(list -> {
// do something with repo list
}, Throwable::printStackTrace);
by Nick Fedesna