You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.2 KiB
Java

2 years ago
package com.rehome.zhdcoa.utils;
import android.app.Activity;
2 years ago
import android.text.TextUtils;
2 years ago
import android.util.Log;
import com.google.gson.Gson;
import com.rehome.zhdcoa.App;
2 years ago
import com.rehome.zhdcoa.BuildConfig;
2 years ago
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.download.DownloadQueue;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.RequestQueue;
/**
* Created by ruihong on 2017/10/25.
*/
public class NohttpUtils {
private static NohttpUtils sUtils;
private final RequestQueue mQueue;
/**
* .
*/
private static DownloadQueue downloadQueue;
private NohttpUtils() {
mQueue = NoHttp.newRequestQueue(8);
}
/**
* DCL
*
* @return
*/
public static NohttpUtils getInstance() {
if (sUtils == null) {
synchronized (NohttpUtils.class) {
if (sUtils == null) {
sUtils = new NohttpUtils();
}
}
}
return sUtils;
}
/**
* .
*/
public static DownloadQueue getDownloadInstance() {
if (downloadQueue == null)
downloadQueue = NoHttp.newDownloadQueue(1);
return downloadQueue;
}
/**
*
*
* @param mActivity activity
* @param what
* @param request
* @param callback
* @param canCanel
* @param isLoading
* @param <T>
*/
public <T> void add(Activity mActivity, int what, Request<T> request, HttpListener<T> callback, boolean canCanel, boolean isLoading, String msg) {
if(App.getInstance().getUserInfo()!=null&&App.getInstance().getUserInfo().getToken()!=null){
String token = App.getInstance().getUserInfo().getToken();
String credential = "Bearer " + token;
request.addHeader("Authorization", credential);
2 years ago
showLog(request.url());
// showLog(credential);
// showLog(new Gson().toJson(request.headers()));
2 years ago
}
mQueue.add(what, request, new HttpResponseListener<T>(mActivity, request, callback, canCanel, isLoading, msg));
}
public <T> void add(Activity mActivity, int what, Request<T> request, HttpListener<T> callback) {
if(App.getInstance().getUserInfo()!=null&&App.getInstance().getUserInfo().getToken()!=null){
String token = App.getInstance().getUserInfo().getToken();
String credential = "Bearer " + token;
request.addHeader("Authorization", credential);
2 years ago
showLog(request.url());
// showLog(credential);
// showLog(new Gson().toJson(request.headers()));
2 years ago
}
mQueue.add(what, request, new HttpResponseListener<T>(mActivity, request, callback, true, true, "加载中..."));
}
public <T> void add(Activity mActivity, int what, String progressTitle, Request<T> request, HttpListener<T> callback) {
if(App.getInstance().getUserInfo()!=null&&App.getInstance().getUserInfo().getToken()!=null){
String token = App.getInstance().getUserInfo().getToken();
String credential = "Bearer " + token;
request.addHeader("Authorization", credential);
2 years ago
showLog(request.url());
// showLog(credential);
// showLog(new Gson().toJson(request.headers()));
2 years ago
}
mQueue.add(what, request, new HttpResponseListener<T>(mActivity, request, callback, true, true, progressTitle));
}
2 years ago
public void showLog(String logText) {
if (BuildConfig.LOG_ERROR) {
if(TextUtils.isEmpty(logText)){
Log.i("app", "logText is null");
}else{
Log.i("app", logText);
}
}
}
2 years ago
/**
* sign
*
* @param sign
*/
public void cancelBySign(Object sign) {
mQueue.cancelBySign(sign);
}
/**
*
*/
public void cancelAll() {
mQueue.cancelAll();
}
/**
* 退App
*/
public void stopAll() {
mQueue.stop();
}
}