반응형
OkHttp 라이브러리를 이용해서 유저정보를 호출했다.
OkHttpClient client2 = new OkHttpClient().newBuilder()
.build();
Request request3 = new Request.Builder()
.url("https://panasonic1.talentlms.com/api/v1/users/id:"+userBean.getUser_id())
.method("GET", null)
.addHeader("Authorization", "Basic RFRDejRxWVI1eE1RemNwQjVVbktQWTFpZHNzR0lwOg==")
.addHeader("Cookie", "AWSALB=BMrb5JNU1MJMwLUZDnqSqyGhxcUQwfYVos2nthPQ8EU+41COg65UeDedxNZeQsw0gfeqPKKhI2VTDxw2HIiqqPe6T+Oi57IY/EtEOKraj9oPqNn2QFF1f0Dtr9rFhVWKfu1vfkBCTNIVRxXnGKDXBYFWXNtcBP+YYLb5PSTukcAb+6z2cWgqaD4XnwfJhw==; AWSALBCORS=BMrb5JNU1MJMwLUZDnqSqyGhxcUQwfYVos2nthPQ8EU+41COg65UeDedxNZeQsw0gfeqPKKhI2VTDxw2HIiqqPe6T+Oi57IY/EtEOKraj9oPqNn2QFF1f0Dtr9rFhVWKfu1vfkBCTNIVRxXnGKDXBYFWXNtcBP+YYLb5PSTukcAb+6z2cWgqaD4XnwfJhw==; PHPSESSID=elb~qdoi8nqik68t0jrc2hnkkma6om")
.build();
Response response3 = client.newCall(request3).execute();
response3의 객체정보를 print했더니
System.out.println(response3.getClass().getName());
-> okhttp3.Response 즉 Response 타입인 것을 확인했다.
이 Response형을 String형으로 변환하고 다시 json형으로 변환한다.
//Response형 -> String형
String userString = response3.body().string();
// String형 ->json형
JSONObject userJson = new JSONObject(userString);
이렇게 얻은 json형의 유저정보를 출력하면
System.out.println(userJson);
{"login_key":
"4710239847209318479028314790821347",
"timezone":"(GMT +09:00) Osaka, Sapporo, Tokyo, Japan","bio":"",
"language":"Korean",
"login":"tester",
"deactivation_date":"",
"points":"75",
"custom_field_12":"tester",
"user_type":"Learner-Type",
"custom_field_10":"tester",
"custom_field_11":"tester",
"last_updated_timestamp":"1637067730",
"id":"108",
"first_name":"tester",
"email":"tester@naver.com",
"courses":[{"completion_percentage":"0","role":"learner","expired_on_timestamp":null,"completed_on":"","completed_on_timestamp":null,"enrolled_on_timestamp":"1636445395","last_accessed_unit_url":"https://panasonic1.talentlms.com/course/lastaccessedunit/course_id:131,user_id:108","total_time_seconds":62,"enrolled_on":"09/11/2021, 17:09:55","name":"IPD(화상처리기)","completion_status":"incomplete","id":"131","total_time":"1m 2s","completion_status_formatted":"In progress","expired_on":""},
{"completion_percentage":"0","role":"learner","expired_on_timestamp":null,"completed_on":"","completed_on_timestamp":null,"enrolled_on_timestamp":"1636600560","last_accessed_unit_url":"https://panasonic1.talentlms.com/course/lastaccessedunit/course_id:130,user_id:108","total_time_seconds":0,"enrolled_on":"11/11/2021, 12:16:00","name":"PLC","completion_status":"not_attempted","id":"130","total_time":null,"completion_status_formatted":"Not started","expired_on":""},
{"completion_percentage":"0","role":"learner","expired_on_timestamp":null,"completed_on":"","completed_on_timestamp":null,"enrolled_on_timestamp":"1635832492","last_accessed_unit_url":"https://panasonic1.talentlms.com/course/lastaccessedunit/course_id:126,user_id:108","total_time_seconds":0,"enrolled_on":"02/11/2021, 14:54:52","name":"강의_ 대리점용","completion_status":"not_attempted","id":"126","total_time":null,"completion_status_formatted":"Not started","expired_on":""},
{"completion_percentage":"0","role":"learner","expired_on_timestamp":null,"completed_on":"","completed_on_timestamp":null,"enrolled_on_timestamp":"1635818888","last_accessed_unit_url":"https://panasonic1.talentlms.com/course/lastaccessedunit/course_id:127,user_id:108","total_time_seconds":0,"enrolled_on":"02/11/2021, 11:08:08","name":"강의_본사용","completion_status":"not_attempted","id":"127","total_time":null,"completion_status_formatted":"Not started","expired_on":""}],"last_updated":"16/11/2021, 22:02:10","level":"1","last_name":"han","groups":[{"name":"test_Group","id":"34"}],
"avatar":"https://d3j0t7vrtr92dk.cloudfront.net/images/default_user.png",
"custom_field_9":"tester",
"branches":[],
"certifications":[],
"badges":[{"image_url":"https://d3j0t7vrtr92dk.cloudfront.net/images/badges/set1/activity/1.png","criteria":"4 logins","issued_on_timestamp":1635818957,"name":"Activity Newbie","issued_on":"02/11/2021, 11:09:17","type":"activity","badge_set_id":"1"},{"image_url":"https://d3j0t7vrtr92dk.cloudfront.net/images/badges/set1/activity/2.png","criteria":"8 logins","issued_on_timestamp":1635832042,"name":"Activity Grower","issued_on":"02/11/2021, 14:47:22","type":"activity","badge_set_id":"1"}],"created_on":"01/11/2021, 15:11:05","restrict_email":"0","status":"active"
}
-> 다음과 같은 유저정보가 json 형식으로 나온다.
이제부터 할 것은
1) 이 json데이터에서 특정정보(강의)만을 추출해서 array형태로 변환후
2) 반복문을 돌리며 강의 번호, 강의명, 강의 url를 출력할 것이다.
1) 강의 정보만 추출하기
//json에서 배열가져오기
JSONArray courses = userJson.getJSONArray("courses");
System.out.println(courses);
출력결과
[{"completion_percentage":"0","role":"learner","expired_on_timestamp":null,"completed_on":"","completed_on_timestamp":null,"enrolled_on_timestamp":"1636445395","last_accessed_unit_url":"https://panasonic1.talentlms.com/course/lastaccessedunit/course_id:131,user_id:108","total_time_seconds":62,"enrolled_on":"09/11/2021, 17:09:55","name":"IPD(화상처리기)","completion_status":"incomplete","id":"131","total_time":"1m 2s","completion_status_formatted":"In progress","expired_on":""},
{"completion_percentage":"0","role":"learner","expired_on_timestamp":null,"completed_on":"","completed_on_timestamp":null,"enrolled_on_timestamp":"1636600560","last_accessed_unit_url":"https://panasonic1.talentlms.com/course/lastaccessedunit/course_id:130,user_id:108","total_time_seconds":0,"enrolled_on":"11/11/2021, 12:16:00","name":"PLC","completion_status":"not_attempted","id":"130","total_time":null,"completion_status_formatted":"Not started","expired_on":""},
{"completion_percentage":"0","role":"learner","expired_on_timestamp":null,"completed_on":"","completed_on_timestamp":null,"enrolled_on_timestamp":"1635832492","last_accessed_unit_url":"https://panasonic1.talentlms.com/course/lastaccessedunit/course_id:126,user_id:108","total_time_seconds":0,"enrolled_on":"02/11/2021, 14:54:52","name":"강의_ 대리점용","completion_status":"not_attempted","id":"126","total_time":null,"completion_status_formatted":"Not started","expired_on":""},
{"completion_percentage":"0","role":"learner","expired_on_timestamp":null,"completed_on":"","completed_on_timestamp":null,"enrolled_on_timestamp":"1635818888","last_accessed_unit_url":"https://panasonic1.talentlms.com/course/lastaccessedunit/course_id:127,user_id:108","total_time_seconds":0,"enrolled_on":"02/11/2021, 11:08:08","name":"강의_본사용","completion_status":"not_attempted","id":"127","total_time":null,"completion_status_formatted":"Not started","expired_on":""}]
: 다음과 같이 강의 정보들만 잘 출력됐다.
2) 강의번호, 강의명, 강의 url만 출력하기
for(int i =0; i <courses.length(); i++){
JSONObject object = courses.getJSONObject(i);
String course_id = object.getString("id");
String name = object.getString("name");
String url = object.getString("last_accessed_unit_url");
System.out.println(course_id);
System.out.println(name);
System.out.println(url);
}
출력결과:
130
PLC
https://panasonic1.talentlms.com/course/lastaccessedunit/course_id:130,user_id:108
126
강의_ 대리점용
https://panasonic1.talentlms.com/course/lastaccessedunit/course_id:126,user_id:108
127
강의_본사용
https://panasonic1.talentlms.com/course/lastaccessedunit/course_id:127,user_id:108
다음과 같이 강의정보만 잘 출력됐다!
'Study > JAVA' 카테고리의 다른 글
[JAVA] 오버로딩, 오버라이딩 (0) | 2022.11.05 |
---|---|
[Java] 접근제어자 (0) | 2020.06.18 |
[JAVA] 재귀함수 (0) | 2020.06.15 |
[Java] 조건문 문제풀기-2 (0) | 2020.06.15 |
[JAVA] 조건문 문제풀기 (0) | 2020.06.15 |