티스토리 뷰

Volley 에러메시지 확인하기

Dexx 2017. 6. 8. 15:31

Volley 라이브러리를 이용해 개발 진행 시에 리턴되는 에러 메시지가 제대로 표시 되지 않을때가 있다.

이럴때 onErrorResponse에서 해당 내용을 표시해 주면 더 쉽게 이해가 될텐데 그렇지 않아 이유를 알아보는데 한참 걸렸다.


방법은 다음과 같다.


1
2
3
4
@Override
public void onErrorResponse(VolleyError error) {
    Log.d(TAG, "onErrorResponse : " + String.valueOf(error));
}
cs


여지껏 보통 이렇게 에러 메시지를 확인 했다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Override
public void onErrorResponse(VolleyError error) {
    NetworkResponse response = error.networkResponse;
    if (error instanceof ServerError && response != null) {
        try {
            String res = new String(response.data, HttpHeaderParser.parseCharset(response.headers, "utf-8"));
            //JSONObject obj = new JSONObject(res);
            //리턴값이 Json일 때
            Log.d(TAG, res);
            } catch (UnsupportedEncodingException e1) {
                e1.printStackTrace();
            }
            //catch (JSONException e2) {
                //returned data is not JSONObject?
                //e2.printStackTrace();
            //}
    }
    Log.d(TAG, "onErrorResponse : " + String.valueOf(error));
}
cs


에러 메시지를 String으로 변환하여 보여주도록 처리

Json일 때는 한번더 처리해야 한다.



'Android 개발 메모' 카테고리의 다른 글

(맥) 페이스북 Hash Key 생성  (0) 2017.06.13
나눔바른고딕 경량화 폰트  (0) 2017.06.02
android 아이콘 사이즈  (0) 2017.04.04
댓글