Created
April 3, 2015 10:33
-
-
Save unosk/9913969eec7edc922d5c to your computer and use it in GitHub Desktop.
VolleyErrorHelper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<string name="error_general">エラーが発生しました</string> | |
<string name="error_timeout">接続がタイムアウトしました</string> | |
<string name="error_server">接続先でエラーが発生しました</string> | |
<string name="error_network">インターネットに接続していません</string> | |
<string name="error_auth">認証できませんでした</string> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import com.android.volley.AuthFailureError; | |
import com.android.volley.NetworkError; | |
import com.android.volley.NoConnectionError; | |
import com.android.volley.ServerError; | |
import com.android.volley.TimeoutError; | |
public class VolleyErrorHelper { | |
public static String getMessage(Context context, Object error) { | |
return context.getResources().getString(getMessageResource(error)); | |
} | |
private static int getMessageResource(Object error) { | |
if (isTimeoutProblem(error)) { | |
return R.string.error_timeout; | |
} else if (isNetworkProblem(error)) { | |
return R.string.error_network; | |
} else if (isAuthProblem(error)) { | |
return R.string.error_auth; | |
} else if (isServerProblem(error)) { | |
return R.string.error_server; | |
} else { | |
return R.string.error_general; | |
} | |
} | |
private static boolean isTimeoutProblem(Object error) { | |
return error instanceof TimeoutError; | |
} | |
private static boolean isNetworkProblem(Object error) { | |
return error instanceof NetworkError || error instanceof NoConnectionError; | |
} | |
private static boolean isAuthProblem(Object error) { | |
return error instanceof AuthFailureError; | |
} | |
private static boolean isServerProblem(Object error) { | |
return error instanceof ServerError; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment