Skip to content

Instantly share code, notes, and snippets.

@leiweibo
Created January 27, 2016 08:19
Show Gist options
  • Save leiweibo/76f73200f59e1a98282e to your computer and use it in GitHub Desktop.
Save leiweibo/76f73200f59e1a98282e to your computer and use it in GitHub Desktop.
/**
* Created by weibo on 15-12-2.
*/
public abstract class NetworkCallback<T> implements Callback<T> {
private RecyclerViewHelper.Action action;
private Context context;
public NetworkCallback(Context context) {
this.context = context;
}
@Override
public void onResponse(Response<T> response, Retrofit retrofit) {
onNetworkRequestFinished();
if (response.isSuccess()) {
onSuccess(response.body());
} else {
onFailed(response, retrofit);
}
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
onNetworkRequestFinished();
JToast.toastLong(context.getString(R.string.network_error));
}
public void onFailed(Response response, Retrofit retrofit) {
Converter<ResponseBody, JErrorReponse> errorConverter
= retrofit.responseConverter(JErrorReponse.class, new Annotation[0]);
try {
JErrorReponse error = errorConverter.convert(response.errorBody());
if (error != null) {
JToast.toastLong(error.msg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public RecyclerViewHelper.Action getAction() {
return this.action;
}
public void setAction(RecyclerViewHelper.Action action) {
this.action = action;
}
public abstract void onSuccess(T t);
private void onNetworkRequestFinished() {
if (context != null && context instanceof JFragmentActivity) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
((JFragmentActivity) context).dialogHelper.dismissProgressDialog();
}
}, 500);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment