본문 바로가기
Android

retrofit2 okhttp response의 글자가 깨진다면

by leopard4 2023. 3. 16.

 

유니코드가 인코딩이 제대로 되지않았기 때문이다.

 

try {
    String errorBody = response.errorBody().string(); // 에러바디를 String으로 변환
    JSONObject jsonObject = new JSONObject(errorBody); // String을 JSON으로 변환
    String errorMessage = jsonObject.getString("error"); // JSON에서 error 키의 값을 가져옴
    Log.i("REGISTER_ACTIVITY1", errorMessage);
    // else if errorMessage == 이미 존재하는 이메일입니다. 이런식으로
    // 오류 메시지 처리
} catch (Exception e) { // 에러바디를 String으로 변환하는 과정에서 에러가 발생할 수 있음
    e.printStackTrace();
    // 오류 처리
}