[ Developer ]/Android
[Android] 안드로이드 웹 서버 응답 받기 Android to Spring
김현섭.
2016. 8. 1. 22:05
Android to Spring Gson으로 Response Data 받기
Gson을 이용하기 위해서 build.gradle에서 complie을 추가해준다
1
2
3
4
5
6
|
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.google.code.gson:gson:2.7'
}
|
cs |
위와 같이 추가한 후 Sync Now를 통해서 추가해준다
이전에 Response로 받았던 JSON을 받아서 사용한다
객체로 받기 위해서 java에 Data라는 클래스를 선언해서 받는 형식으로 정의를 해준다
그런 후 onPostExecute에서 Gson을 이용해서 JSON 데이터를 객체화 시킨다
1
2
3
4
5
6
7
8
9
10
|
@Override
protected void onPostExecute(String s) {
Log.d("JSON_RESULT", s);
Gson gson = new Gson();
Data data = gson.fromJson(s, Data.class);
Log.d("JSON_RESULT", data.getData1());
Log.d("JSON_RESULT", data.getData2());
}
|
cs |
위와 같이 Gson으로 받아서 출력을 해본다
다음과 같은 결과를 얻는다
그런 후 Activity로 출력을 해주기 위해서 activity_Main에서 TextView를 2개 추가해준다
그런 후 Main에서 TextView를 추가한다
그런 후 onPostExecute에서 set을 시켜주면 된다
다음과 같은 결과를 얻는다