声明消息队列:(单例模式)
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.RequestQueue;
/**
* 请求队列
*/
public class CallServer {
private static CallServer instance;
// 消息队列
private RequestQueue rq;
// 构造函数
// 声明一个新的消息队列
private CallServer() {
rq = NoHttp.newRequestQueue();
}
// 实例化CallServer
public synchronized static CallServer getInstance() {
if (instance == null)
synchronized (CallServer.class) {
if (instance == null)
instance = new CallServer();
}
return instance;
}
/**
* 添加一个请求到请求队列。
*
* @param what 用来标志请求, 当多个请求使用同一个Listener时, 在回调方法中会返回这个what。
* @param request 请求对象。
* @param listener 结果回调对象。
*/
//
public <T> void add(int what, Request<T> request, OnResponseListener<T> listener) {
rq.add(what, request, listener);
}
/**
* 取消这个sign标记的所有请求。
*
* @param sign 请求的取消标志。
*/
public void cancelBySign(Object sign) {
rq.cancelBySign(sign);
}
/**
* 取消队列中所有请求。
*/
public void cancelAll() {
rq.cancelAll();
}
}
BaseActivity里的request( )函数:
private Object cancelObject = new Object();
// 网络申请
public <T> void request(int what, Request<T> request, NoHttpListener<T> httpListener) {
// 这里设置一个sign给这个请求。
request.setCancelSign(cancelObject);
// 核心代码
CallServer.getInstance().add(what, request, new NoHttpManager<T>(request,
httpListener));
}
OnResponseListener接口:(框架定义的)
package com.yolanda.nohttp.rest;
/**
* Created in Jul 28, 2015 7:32:53 PM.
*
* @param <T> a generic, on behalf of you can accept the result type, .It should be with the {@link Request}, {@link Response}.
* @author Yan Zhenjie.
*/
public interface OnResponseListener<T> {
/**
* When the request starts.
*
* @param what the credit of the incoming request is used to distinguish between multiple requests.
*/
void onStart(int what);
/**
* Server correct response to callback when an HTTP request.
*
* @param what the credit of the incoming request is used to distinguish between multiple requests.
* @param response successful callback.
*/
void onSucceed(int what, Response<T> response);
/**
* When there was an error correction.
*
* @param what the credit of the incoming request is used to distinguish between multiple requests.
* @param response failure callback.
*/
void onFailed(int what, Response<T> response);
/**
* When the request finish.
*
* @param what the credit of the incoming request is used to distinguish between multiple requests.
*/
void onFinish(int what);
}
NoHttpManager类:
NoHttpMahager实现OnResponseListener接口
public class NoHttpManager<T> implements OnResponseListener<T> {
private NoHttpListener<T> mListener;
private Request<T> mRequest;
public NoHttpManager(Request<T> request, NoHttpListener<T>
httpListener) {
mListener = httpListener;
this.mRequest = request;
}
@Override
public void onStart(int what) {
if (mListener != null) {
mListener.onStart(what);
}
}
@Override
public void onSucceed(int what, Response<T> response) {
if (response.responseCode() == 401) {
EventBus.getDefault().post(new LoginEvent());
return;
}
if (mListener != null) {
mListener.onSucceed(what, response);
}
}
@Override
public void onFailed(int what, Response<T> response) {
LogUtils.LOGE("Exception", response.getException().toString());
ToastUtil.showToastBottom(XdApp.getAppContext(), XdApp.getAppContext().getString(R.string.request_faile));
if (mListener != null) {
mListener.onFailed(what, response);
}
}
@Override
public void onFinish(int what) {
if (mListener != null) {
mListener.onFinish(what);
}
}
}
NoHttpListener接口:
import com.yolanda.nohttp.rest.Response;
public interface NoHttpListener<T> {
void onStart(int what);
void onFinish(int what);
void onSucceed(int what, Response<T> response);
void onFailed(int what, Response<T> response);
}
函数的使用:
private void server(String id){
Map<String, String> param = new HashMap<>();
param.put("pro_id", id + "");
request(1, NoHttpUtils.fastJsonObjectRequest(Api.PRO_DETAIL, RequestMethod.GET, param), new NoHttpListener<JSONObject>() {
@Override
public void onStart(int what) {
}
@Override
public void onSucceed(int what, Response<JSONObject> response) {
JSONObject json = response.get();
int code = json.getInteger("code");
String msg = json.getString("service");
if (code != 1) {
ToastUtil.showToast(GoodDetailActivity.this, "不好意思,客服没有留电话。惊不惊喜?");
return;
}else{
MaterialDialog.Builder dialog = DialogUtils.SimpleDialog(GoodDetailActivity.this, "联系客服", msg, "取消", "确定");
dialog.onNegative(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
dialog.dismiss();
}
}).onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
dialog.dismiss();
Intent intent = new Intent(Intent.ACTION_DIAL);
Uri data = Uri.parse("tel:" + msg);
intent.setData(data);
startActivity(intent); }
}).show();
}
}
@Override
public void onFailed(int what, Response<JSONObject> response) {
}
@Override
public void onFinish(int what) {
}
});
}
核心的代码:
CallServer.getInstance().add(what, request, new NoHttpManager<T>(request,
httpListener));
// CallServer是单例模式下的消息序列
// request是请求队列
// NoHttpManager是实现的OnPesponseListener接口的实现类 通过httpListener是实现回调监听器
// httpListener是真正的回调监听器,让用户自定义事件
https://blog.csdn.net/sanciyuan2044123845/article/details/80627000