如何使用AsyncTask自定义Retrofit?

时间:2015-02-12 08:03:19

标签: android android-asynctask retrofit

我使用Retrofit。有时我想与AsyncTask集成用于某些目的,比如Dialog Loading ...

这是我的方式:

public class RetrofitAsyncTask<T extends ResponseModel> {
public interface OnCompleteListener<T extends ResponseModel> {
    public void onComplete(T t);
    public void onError(Exception e);
}

public interface JsonConverter<T extends ResponseModel> {
    public T getModel();
}

protected OnCompleteListener<T> onCompleteListener = null;
protected JsonConverter<T> jsonConverter;
protected ProgressDialog progressDialog;
protected int requestCode = Integer.MIN_VALUE;

public static RetrofitAsyncTask newInstance() {
    return new RetrofitAsyncTask();
}

public RetrofitAsyncTask setOnCompleteListener(OnCompleteListener<T> l) {
    onCompleteListener = l;
    return this;
}

public RetrofitAsyncTask setJsonConverter(JsonConverter<T> converter) {
    jsonConverter = converter;
    return this;
}


public RetrofitAsyncTask setProgressDialog(ProgressDialog dlg) {
    progressDialog = dlg;
    return this;
}

public RetrofitAsyncTask setRequestCode(int reqCode) {
    requestCode = reqCode;
    return this;
}

public void execute() {
    new AsyncTask<Void, Void, Exception>() {
        private T result;

        @Override
        protected ProgressDialog getDialogRunOnPreStart() {
            /** Retrun dialog will display. Will process on onPre & onPost. But current I will ignore it.**/
            return progressDialog;
        }

        @Override
        protected Exception doInBackground(Void... params) {
            if (jsonConverter == null) {
                return null;
            }
            try {
                result = jsonConverter.getModel();
                result.setRequestCode(requestCode);
            } catch (Exception e) {
                return e;
            }
            return null;
        }

        @Override
        protected void onPostExecute(Exception e) {
            if (onCompleteListener != null) {
                if (e != null || result == null) {
                    onCompleteListener.onError(e);
                } else {
                    onCompleteListener.onComplete(result);
                }
            }
        }
    }.execute();
}

}

在这种情况下,类ResponseModel是Empty class。

public class ResponseModel {/** You will custom some filed default here **/}

接下来,您将定义一些Service Api ....看起来像:

public interface MyService {
@GET("/path")
public User getUserInfo(....);
}

(Notes类用户必须从ResponseModel扩展)。 然后你会定制一些RetrofitAdapter ......

最后要使用RetrofitAsyncTask,您将执行以下操作:

RetrofitAsyncTask.newInstance()
                .setJsonConverter(
                        new RetrofitAsyncTask.JsonConverter<User>() {
                            @Override
                            public User getModel() {
                                return MyService.getUser(...);
                            }
                        }
                )
                .setOnCompleteListener(this)
                .setRequestCode(requestCode)
                .execute();

这就是我的方式。但目前我觉得这不好。 (setJsonConverter得到更多的一句话:|,如果你有任何想法让它变得更好而且更短,请评论!非常感谢!)

0 个答案:

没有答案