今やどこの現場でもretrofit2を使ってhttp requestを叩いている気がするので、
使い方のメモ書きをします。
まずは、Gradleファイルへの追加から
| 1 2 3 4 | dependencies {     compile 'com.squareup.retrofit2:retrofit:2.1.0'     compile 'com.squareup.retrofit2:converter-gson:2.1.0' } | 
今回gson convertもかけたいので、一緒にgradleに追加しました。
POSTでリクエストを送る例です。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | package hoge; import retrofit2.Call; import retrofit2.http.Body; import retrofit2.http.GET; import retrofit2.http.POST; import retrofit2.http.Path; import retrofit2.http.Query; public interface Signup {     @POST("signup")     Call<Void> signUp(@Body UserRequest user); } | 
Signupというjavaファイルを作りました。
そこにPOSTメソッドのsignUp Apiを定義します。
このAPIは以下のJson形式のデーターが求められます。
| 1 2 3 4 5 6 7 8 | {   "user":{ 	"name":"name",     "email":"email",     "password":"password",     "password_confirmation":"password"   } } | 
このデータ形式を当てはめてあげるために、新しくクラスを定義します。
requestの元となるRequestUserクラスと
入れ物となるUserクラスです。
| 1 2 3 4 5 6 7 8 9 10 | import com.google.gson.annotations.Expose; public class RequestUser {     @Expose     User user;     public void setUser(User user){         this.user = user;     } } | 
続いて、入れ物となるUserクラス
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | public class User {     public User(){};     public User(String name,String email,String password,String password_confirmation){         this.name = name;         this.email = email;         this.password = password;         this.password_confirmation = password_confirmation;     }     @Expose     private String name;     @Expose     private String email;     @Expose     private String password;     @Expose     private String password_confirmation;     public String getName() {         return name;     }     public void setName(String name) {         this.name = name;     }     public String getEmail() {         return email;     }     public void setEmail(String email) {         this.email = email;     }     public String getPassword() {         return password;     }     public void setPassword(String password) {         this.password = password;     }     public String getPassword_confirmation() {         return password_confirmation;     }     public void setPassword_confirmation(String password_confirmation) {         this.password_confirmation = password_confirmation;     } } | 
入れ物ができたので、retrofit2を使ったrequest部分を作ります。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |     private void callSignUpApi(){         Login login = RetrofitUtils.build().create(Login.class);         RequestUser requestUser = new RequestUser();         User user = new User();         user.setName("hogehoge");         user.setEmail("email@gmail.com);         user.setPassword("hogehoge");         user.setPassword_confirmation("hogehoge");         requestUser.setUser(user);         login.signUp(requestUser).enqueue(new Callback<Void>() {             @Override             public void onResponse(Call<Void> call, Response<Void> response) {                 if(response.isSuccessful()){                 }                 else{                 }             }             @Override             public void onFailure(Call<Void> call, Throwable t) {             }         });     } | 
Responseは空です。
RetrofitUtilsは共通部を定義したstatic classです。
このあたりは共通化するほうが多いみたいです。
他にもいい実装はいくらでもありそうですが
| 1 2 3 4 5 6 7 8 9 | public class RetrofitUtils {     public static Retrofit build(){         return new Retrofit.Builder()                 .baseUrl("http://153.126.214.7/")                 .addConverterFactory(GsonConverterFactory.create())                 .build();     } } | 
以上、Jsonを利用したPostリクエストでした。