Bhavdip
9/1/2015 - 4:54 AM

AdvanceCallback.java


import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Retrofit callback Wrapper class for handle the rest client web service
 * reponse.
 * 
 * @author CreoleStudios
 * 
 * @param <T>
 */
public abstract class AdvanceCallback<T extends CmtWsResponse> implements
        Callback<T> {

	@Override
	public void success(T t, Response response) {
		if (t.getStatus()) {
			success(t);
		} else {
			fail(t);
		}
	}

	@Override
	public void failure(RetrofitError retrofitError) {
		fail(retrofitError);
	}

	public abstract void success(T data);

	public abstract void fail(T failure);
	
	public abstract void fail(RetrofitError retrofitError);
}
public class Exception extends Exception {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String message = null;
	
	public CheckMyTensionException() {
        super();
    }
 
    public CheckMyTensionException(String message) {
        super(message);
        this.message = message;
    }
 
    public CheckMyTensionException(Throwable cause) {
        super(cause);
    }
 
    @Override
    public String toString() {
        return message;
    }
 
    @Override
    public String getMessage() {
        return message;
    }
}
import com.creativecybernetics.android.aib.AIBalancedApp;
import com.creativecybernetics.android.aib.R;
import com.creativecybernetics.android.aib.utility.ConnectivityHelper;

import java.net.ConnectException;

import retrofit.ErrorHandler;
import retrofit.RetrofitError;

public class RetrofitErrorHandler implements ErrorHandler {

    @Override
    public Throwable handleError(RetrofitError retrofitError) {
        if (!ConnectivityHelper.getInstance().isConnected(AIBalancedApp.getInstance().getApplicationContext())) {
            return new CheckMyTensionException(AIBalancedApp.getInstance().getResources().getString(R.string.lbl_no_internet_available));
        } else if (retrofitError.getCause() instanceof ConnectException) {
            return new CheckMyTensionException(AIBalancedApp.getInstance().getResources().getString(R.string.lbl_no_internet_available));
        } else if (retrofitError.getCause() instanceof java.net.SocketTimeoutException) {
            return new CheckMyTensionException(AIBalancedApp.getInstance().getResources().getString(R.string.lbl_server_not_rechable));
        } else if (retrofitError.getCause() instanceof java.net.UnknownHostException) {
            return new CheckMyTensionException(AIBalancedApp.getInstance().getResources().getString(R.string.lbl_server_not_available));
        } else {
            return new CheckMyTensionException(AIBalancedApp.getInstance().getResources().getString(R.string.lbl_internal_problem));
        }
    }

}