Retrofit2 예제
안전한 타입의 HTTP Client 라이브러리로 Android 및 Java 애플리케이션에서 사용합니다
최소요구사항 : Java 8+ or Android API 21+
github : https://github.com/square/retrofit
build.gradle (:app)
1
2
3
4
|
// retrofit2
implementation group: 'com.squareup.retrofit2', name: 'retrofit', version: '2.8.1'
implementation group: 'com.squareup.retrofit2', name: 'converter-gson', version: '2.8.1' // JSON을 직렬화
|
-
Retrofit2 라이브러리 추가
AndroidManifest.xml
1
|
<uses-permission android:name="android.permission.INTERNET" />
|
-
인터넷 사용 권한 추가
ApiInterface.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;
public interface ApiInterface {
// base_url + "api/login" 으로 POST 통신
@POST("api/login")
Call<ResLoginData> requestPostLogin(@Body ReqLoginData reqLoginData ); // @Body : request 파라미터
// base_url + "api/users" 으로 GET 통신
@GET("api/users")
Call<ResUsersData> requestGetUsersDetail( @Query(value = "page", encoded = true) String page ); // @Query : url에 쿼리 파라미터 추가, encoded - true
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
-
HTTP 통신 인터페이스
HttpClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import retrofit2.Retrofit;
public class HttpClient {
private static Retrofit retrofit;
// Http 통신을 위한 Retrofit 객체반환
public static Retrofit getRetrofit() {
if( retrofit == null )
{
Retrofit.Builder builder = new Retrofit.Builder();
builder.baseUrl( "https://reqres.in/" );
builder.addConverterFactory( GsonConverterFactory.create() ); // 받아오는 Json 구조의 데이터를 객체 형태로 변환
retrofit = builder.build();
}
return retrofit;
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
-
Retrofit 객체를 생성
ReqLoginData.java
1
2
3
4
5
6
7
8
9
10
11
|
// api/login 요청 데이터
public class ReqLoginData {
String email;
String password;
public ReqLoginData( String email, String password ) {
this.email = email;
this.password = password;
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
-
api/login 통신의 요청 객체
ResLoginData.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import androidx.annotation.NonNull;
// api/login 응답 데이터
public class ResLoginData {
@Expose
String token;
@NonNull
@Override
public String toString() {
return "[ResLoginData] token=" + token;
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
-
api/login 통신의 응답 객체
-
api/login 통신 결과값을 ResLoginData 객체에 맵핑한다
ResUsersData.java
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
import java.util.List;
import androidx.annotation.NonNull;
// api/users 응답 데이터
public class ResUsersData {
@Expose
int page;
@Expose
@SerializedName("per_page")
int perPage;
@Expose
int total;
@Expose
@SerializedName("total_pages")
int totalPages;
@Expose
List<Data> data;
@Expose
Ad ad;
public class Data {
int id;
String email;
@SerializedName("first_name")
String firstName;
@SerializedName("last_name")
String lastName;
String avatar;
@NonNull
@Override
public String toString() {
return "{id=" + id + " , email=" + email + " , firstName=" + firstName + " , lastName=" + lastName + " , avatar}";
}
}
public class Ad {
String company;
String url;
String text;
@NonNull
@Override
public String toString() {
return "{company=" + company + " , url=" + url + " , text=" + text + "}";
}
}
@Override
public String toString() {
String str = "[ResUsersData] page=" + page + " , perPage=" + perPage + " , total=" + total + " , totalPages=" + totalPages + " , data= [";
str += ",";
}
}
str += "]";
str += " , ad=" + ad.toString();
return str;
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
-
api/users 통신의 응답 객체
-
api/users 통신 결과값을 ResUsersData 객체에 맵핑한다
MainActivity.java
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static String TAG = "MainActivity";
ApiInterface api;
// ui
Button btnGet;
Button btnPost;
TextView txtResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGet = (Button)findViewById( R.id.btnGet );
btnGet.setOnClickListener( this );
btnPost = (Button)findViewById( R.id.btnPost );
btnPost.setOnClickListener( this );
txtResult = (TextView)findViewById( R.id.txtResult );
api = HttpClient.getRetrofit().create( ApiInterface.class );
}
@Override
public void onClick(View view) {
switch ( view.getId() ) {
case R.id.btnPost:
txtResult.setText("");
requestPost();
break;
case R.id.btnGet:
txtResult.setText("");
requestGet();
break;
}
}
// POST 통신요청
public void requestPost() {
Call<ResLoginData> call = api.requestPostLogin( reqLoginData );
// 비동기로 백그라운드 쓰레드로 동작
call.enqueue( new Callback<ResLoginData>() {
// 통신성공 후 텍스트뷰에 결과값 출력
@Override
public void onResponse(Call<ResLoginData> call, Response<ResLoginData> response) {
}
@Override
public void onFailure(Call<ResLoginData> call, Throwable t) {
txtResult.setText( "onFailure" );
}
} );
}
// GET 통신요청
public void requestGet() {
Call<ResUsersData> call = api.requestGetUsersDetail( "2" );
// 비동기로 백그라운드 쓰레드로 동작
call.enqueue(new Callback<ResUsersData>() {
// 통신성공 후 텍스트뷰에 결과값 출력
@Override
public void onResponse(Call<ResUsersData> call, Response<ResUsersData> response) {
txtResult.setText( response.body().toString() );
}
// 통신실패
@Override
public void onFailure(Call<ResUsersData> call, Throwable t) {
txtResult.setText( "onFailure" );
}
});
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
-
POST통신과 GET통신 결과값을 TextView에 출력
activity_main.xml
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
|
<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnPost"
android:text="POST"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnGet"
android:text="GET"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="@+id/txtResult"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
결과
'IT > - 프로그래밍' 카테고리의 다른 글
Android WebView를 스크롤하면 툴바 숨기기 (0) | 2020.04.28 |
---|---|
Android Glide 라이브러리로 gif이미지 로딩 (0) | 2020.04.22 |
GCM 구현2 - 서버 (0) | 2018.08.11 |
GCM 구현1 - Android (0) | 2018.08.11 |
Android Google의 Mobile Vision을 이용한 바코드 스캐너 만들기 (0) | 2018.07.21 |