Skip to content

Instantly share code, notes, and snippets.

@unosk
Created April 3, 2015 10:33
Show Gist options
  • Save unosk/9913969eec7edc922d5c to your computer and use it in GitHub Desktop.
Save unosk/9913969eec7edc922d5c to your computer and use it in GitHub Desktop.
VolleyErrorHelper
<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>
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