Android Open Source - SeeKampf Volley Error Helper






From Project

Back to project page SeeKampf.

License

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.

Java Source Code

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);
  }
}




Java Source Code List

net.avedo.seekampf.BuildConfig.java
net.avedo.seekampf.core.ChangeLog.java
net.avedo.seekampf.core.CustomAdapter.java
net.avedo.seekampf.core.MainActivity.java
net.avedo.seekampf.core.OceanView.java
net.avedo.seekampf.core.OverScrollerCompat.java
net.avedo.seekampf.core.RestDetailsActivity.java
net.avedo.seekampf.core.ScaleGestureDetectorCompat.java
net.avedo.seekampf.core.VolleyActivity.java
net.avedo.seekampf.core.Zoomer.java
net.avedo.seekampf.fragments.AboutFragment.java
net.avedo.seekampf.fragments.AllianceListFragment.java
net.avedo.seekampf.fragments.AuctionListFragment.java
net.avedo.seekampf.fragments.HomeDetailsFragment.java
net.avedo.seekampf.fragments.HomeFragment.java
net.avedo.seekampf.fragments.IslandDetailsFragment.java
net.avedo.seekampf.fragments.IslandListFragment.java
net.avedo.seekampf.fragments.MessageDetailsFragment.java
net.avedo.seekampf.fragments.MessageListFragment.java
net.avedo.seekampf.fragments.OceanFragment.java
net.avedo.seekampf.fragments.PlayerListFragment.java
net.avedo.seekampf.fragments.RestDetailsFragment.java
net.avedo.seekampf.fragments.RestListFragment.java
net.avedo.seekampf.fragments.SettingsFragment.java
net.avedo.seekampf.models.Alliance.java
net.avedo.seekampf.models.Auction.java
net.avedo.seekampf.models.BaseModel.java
net.avedo.seekampf.models.Island.java
net.avedo.seekampf.models.Message.java
net.avedo.seekampf.models.Player.java
net.avedo.seekampf.utils.AuthGsonRequest.java
net.avedo.seekampf.utils.Constants.java
net.avedo.seekampf.utils.Interfaces.java
net.avedo.seekampf.utils.VolleyErrorHelper.java