Back to project page SeeKampf.
The source code is released under:
GNU General Public License
If you think the Android project SeeKampf listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package net.avedo.seekampf.utils; // w ww. ja v a 2 s.c o m import java.util.HashMap; import java.util.Map; import net.avedo.seekampf.R; import com.android.volley.AuthFailureError; import com.android.volley.NetworkError; import com.android.volley.NetworkResponse; import com.android.volley.NoConnectionError; import com.android.volley.ServerError; import com.android.volley.TimeoutError; import com.android.volley.VolleyError; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import android.content.Context; public class VolleyErrorHelper { public static String getMessage(Object error, Context context) { if (error instanceof TimeoutError) { return context.getResources().getString(R.string.errorRequestTimeout); } else if (isServerProblem(error)) { return handleServerError(error, context); } else if (isAuthProblem(error)) { return context.getResources().getString(R.string.errorAuthenticationFailture); } else if (isNetworkProblem(error)) { return context.getResources().getString(R.string.errorNoNetworkConnection); } return context.getResources().getString(R.string.errorDefault); } 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); } private static String handleServerError(Object err, Context context) { VolleyError error = (VolleyError) err; NetworkResponse response = error.networkResponse; if (response != null) { switch (response.statusCode) { case 404: case 422: case 401: try { HashMap<String, String> result = new Gson().fromJson( new String(response.data), new TypeToken<Map<String, String>>() { }.getType()); if (result != null && result.containsKey("error")) { return result.get("error"); } } catch (Exception e) { e.printStackTrace(); } return error.getMessage(); default: return context.getResources().getString( R.string.errorServerNotReachable); } } return context.getResources().getString(R.string.errorDefault); } }