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.

207 lines
7.1 KiB
Java

package com.rehome.zhdcoa.utils;
import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.google.gson.Gson;
import com.rehome.zhdcoa.App;
import com.rehome.zhdcoa.BuildConfig;
import com.rehome.zhdcoa.R;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.download.DownloadQueue;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.RequestQueue;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.CertificateFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;
/**
* 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);
showLog(request.url());
// showLog(credential);
// showLog(new Gson().toJson(request.headers()));
}
mQueue.add(what, request, new HttpResponseListener<T>(mActivity, request, callback, canCanel, isLoading, msg));
}
/**
* 添加请求
*
* @param mActivity activity
* @param what 请求码,用来区分队列中的请求
* @param token token
* @param request 请求
* @param callback 请求回调
* @param canCanel 是否可以取消请求
* @param isLoading 是否弹出对话框
* @param <T>
*/
public <T> void add(Activity mActivity, int what,String token, Request<T> request, HttpListener<T> callback, boolean canCanel, boolean isLoading, String msg) {
if(token!=null){
String credential = "Bearer " + token;
request.addHeader("Authorization", credential);
showLog(request.url());
}
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);
showLog(request.url());
// showLog(credential);
// showLog(new Gson().toJson(request.headers()));
}
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);
showLog(request.url());
// showLog(credential);
// showLog(new Gson().toJson(request.headers()));
}
mQueue.add(what, request, new HttpResponseListener<T>(mActivity, request, callback, true, true, progressTitle));
}
public <T> void addNoProgress(Context context, 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);
showLog(request.url());
// showLog(credential);
// showLog(new Gson().toJson(request.headers()));
}
mQueue.add(what, request, new HttpResponseListenerNoProgress<T>(request, callback));
}
public void showLog(String logText) {
if (BuildConfig.LOG_ERROR) {
if(TextUtils.isEmpty(logText)){
Log.i("app", "logText is null");
}else{
Log.i("app", logText);
}
}
}
/**
* 取消这个sign这个标记的所有请求
*
* @param sign
*/
public void cancelBySign(Object sign) {
mQueue.cancelBySign(sign);
}
/**
* 取消队列中所有请求
*/
public void cancelAll() {
mQueue.cancelAll();
}
/**
* 退出App停止所有请求
*/
public void stopAll() {
mQueue.stop();
}
public static SSLSocketFactory getSSLSocketFactory(Context context) {
try {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null);
String certificateAlias = Integer.toString(0);
keyStore.setCertificateEntry(certificateAlias, certificateFactory.
generateCertificate(context.getResources().openRawResource(R.raw.domain)));
SSLContext sslContext = SSLContext.getInstance("TLS");
final TrustManagerFactory trustManagerFactory =
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
sslContext.init
(
null,
trustManagerFactory.getTrustManagers(),
new SecureRandom()
);
return sslContext.getSocketFactory();
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
}