kotlin转化2
This commit is contained in:
parent
90200b5348
commit
88be111e35
@ -95,9 +95,9 @@ dependencies {
|
|||||||
implementation 'com.github.jenly1314.AppUpdater:app-updater:1.1.0'
|
implementation 'com.github.jenly1314.AppUpdater:app-updater:1.1.0'
|
||||||
implementation 'com.github.jenly1314.AppUpdater:app-dialog:1.1.0'
|
implementation 'com.github.jenly1314.AppUpdater:app-dialog:1.1.0'
|
||||||
//轮播插件
|
//轮播插件
|
||||||
implementation 'com.github.zhpanvip:BannerViewPager:3.5.4'
|
// implementation 'com.github.zhpanvip:BannerViewPager:3.5.4'
|
||||||
//选择器
|
//选择器
|
||||||
implementation 'com.github.gzu-liyujiang.AndroidPicker:WheelPicker:3.1.1'
|
// implementation 'com.github.gzu-liyujiang.AndroidPicker:WheelPicker:3.1.1'
|
||||||
implementation 'com.github.gzu-liyujiang.AndroidPicker:AddressPicker:3.1.1'
|
implementation 'com.github.gzu-liyujiang.AndroidPicker:AddressPicker:3.1.1'
|
||||||
//EventBus
|
//EventBus
|
||||||
implementation 'org.greenrobot:eventbus:3.1.1'
|
implementation 'org.greenrobot:eventbus:3.1.1'
|
||||||
@ -111,7 +111,7 @@ dependencies {
|
|||||||
implementation 'com.umeng.umsdk:common:9.4.2'// 必选
|
implementation 'com.umeng.umsdk:common:9.4.2'// 必选
|
||||||
implementation 'com.umeng.umsdk:asms:1.4.1'// 必选
|
implementation 'com.umeng.umsdk:asms:1.4.1'// 必选
|
||||||
implementation 'com.umeng.umsdk:apm:1.4.2' // 错误分析升级为独立SDK,看crash数据请一定集成,可选
|
implementation 'com.umeng.umsdk:apm:1.4.2' // 错误分析升级为独立SDK,看crash数据请一定集成,可选
|
||||||
implementation 'com.umeng.umsdk:abtest:1.0.0'//使用U-App中ABTest能力,可选
|
// implementation 'com.umeng.umsdk:abtest:1.0.0'//使用U-App中ABTest能力,可选
|
||||||
//xui
|
//xui
|
||||||
implementation 'com.github.xuexiangjys:XUI:1.1.8'
|
implementation 'com.github.xuexiangjys:XUI:1.1.8'
|
||||||
|
|
||||||
|
27
app/src/main/java/com/oneclouds/cargo/request/net/Api.java
Normal file
27
app/src/main/java/com/oneclouds/cargo/request/net/Api.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package com.oneclouds.cargo.request.net;
|
||||||
|
|
||||||
|
|
||||||
|
import com.dahe.mylibrary.net.CommonResponseBean;
|
||||||
|
import com.oneclouds.cargo.request.bean.LoginRegInputBean;
|
||||||
|
|
||||||
|
import io.reactivex.rxjava3.core.Observable;
|
||||||
|
import retrofit2.http.Body;
|
||||||
|
import retrofit2.http.POST;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Administrator on 2018/8/8 0008.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public interface Api {
|
||||||
|
|
||||||
|
String BASE_URL = "http://app.dahehuoyun.com/api/";
|
||||||
|
|
||||||
|
|
||||||
|
//获取版本
|
||||||
|
String VERSION = BASE_URL + "app/version";
|
||||||
|
|
||||||
|
|
||||||
|
@POST(BASE_URL+"app/bank/getContracts")
|
||||||
|
Observable<CommonResponseBean> log(@Body String bean);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,60 @@
|
|||||||
|
package com.oneclouds.cargo.request.net;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import com.dahe.mylibrary.net.CommonResponseBean;
|
||||||
|
|
||||||
|
import io.reactivex.rxjava3.annotations.NonNull;
|
||||||
|
import io.reactivex.rxjava3.core.Observer;
|
||||||
|
import io.reactivex.rxjava3.disposables.Disposable;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义观察者
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class BaseObserver<T> implements Observer<CommonResponseBean<T>> {
|
||||||
|
private Context mContext;
|
||||||
|
private RxHttpCallBack<T> mRxHttpCallBack;
|
||||||
|
private Disposable disposable;
|
||||||
|
|
||||||
|
public BaseObserver(Context mContext, RxHttpCallBack<T> mRxHttpCallBack) {
|
||||||
|
this.mContext = mContext;
|
||||||
|
this.mRxHttpCallBack = mRxHttpCallBack;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 订阅成功
|
||||||
|
*
|
||||||
|
* @param d
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onSubscribe(@NonNull Disposable d) {
|
||||||
|
this.disposable = d;
|
||||||
|
mRxHttpCallBack.onStart();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onNext(CommonResponseBean<T> tCommonResponseBean) {
|
||||||
|
if (200==tCommonResponseBean.getCode()) {
|
||||||
|
mRxHttpCallBack.onSuccess(tCommonResponseBean);
|
||||||
|
} else {
|
||||||
|
mRxHttpCallBack.onCodeError(mContext, tCommonResponseBean);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(Throwable e) {
|
||||||
|
mRxHttpCallBack.onFailure(mContext, e);
|
||||||
|
if (disposable!=null&&disposable.isDisposed()){
|
||||||
|
disposable.dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onComplete() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,161 @@
|
|||||||
|
package com.oneclouds.cargo.request.net;
|
||||||
|
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.dahe.mylibrary.net.JsonUtils;
|
||||||
|
import com.dahe.mylibrary.utils.MD5Utils;
|
||||||
|
import com.dahe.mylibrary.utils.StringUtils;
|
||||||
|
import com.dahe.mylibrary.utils.TimeUtil;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import okhttp3.MediaType;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Administrator on 2017/4/15 0015.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class CreateJsonUtils {
|
||||||
|
public static CreateJsonUtils utils = null;
|
||||||
|
private static final String TAG = "GetJsonUtils";
|
||||||
|
|
||||||
|
public static CreateJsonUtils getInstance() {
|
||||||
|
if (null == utils) {
|
||||||
|
synchronized (CreateJsonUtils.class) {
|
||||||
|
if (null == utils) {
|
||||||
|
utils = new CreateJsonUtils();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return utils;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到请求体的json数据
|
||||||
|
*
|
||||||
|
* @param key jsonObject的key
|
||||||
|
* @param value jsonObject的value 如果要请求jsonObject里面嵌套jsonObject的value只需要传map 否就传简单的数据类型
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String getRequest(String[] key, Object[] value) {
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
for (int i = 0; i < key.length; i++) {
|
||||||
|
if (value[i] instanceof Map) {
|
||||||
|
Map<Object, Object> sonMap = new HashMap<>();
|
||||||
|
Map<Object, Object> sonDateMap = (Map<Object, Object>) value[i];
|
||||||
|
List<String> keyList = getMapKey(sonDateMap);
|
||||||
|
for (int j = 0; j < keyList.size(); j++) {
|
||||||
|
sonMap.put(keyList.get(j), sonDateMap.get(keyList.get(j)));
|
||||||
|
}
|
||||||
|
map.put(key[i], sonMap);
|
||||||
|
} else if (value[i] instanceof List) {
|
||||||
|
map.put(key[i], value[i]);
|
||||||
|
} else if (value[i] instanceof String) {
|
||||||
|
map.put(key[i], value[i].toString());
|
||||||
|
} else {
|
||||||
|
map.put(key[i], value[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
map.put("api_key", "xccdb-power-bank");
|
||||||
|
map.put("sign", MD5Utils.md5(JsonUtils.getInstance().getGson().toJson(map)));
|
||||||
|
map.put("time", TimeUtil.getNowMills());
|
||||||
|
Log.i(TAG, new Gson().toJson(map));
|
||||||
|
return new Gson().toJson(map);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取map里面的所有值
|
||||||
|
*
|
||||||
|
* @param hashmp
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public List<String> getMapKey(Map hashmp) {
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
Set set = hashmp.keySet();
|
||||||
|
Iterator iter = set.iterator();
|
||||||
|
while (iter.hasNext()) {
|
||||||
|
String key = (String) iter.next();
|
||||||
|
list.add(key);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Integer> getPagerMap(int page, int count) {
|
||||||
|
Map<String, Integer> map = new HashMap<>();
|
||||||
|
map.put("page", page);
|
||||||
|
map.put("count", count);
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 无参数的请求
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Map<String, RequestBody> createRequestBody() {
|
||||||
|
return createRequestBody(new String[]{}, new String[]{});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成请求的body
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* @param value
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Map<String, RequestBody> createRequestBody(String[] key, Object[] value) {
|
||||||
|
Map<String, RequestBody> requestBodyMap = new HashMap<>();
|
||||||
|
Map<String, Object> requestDataMap = new HashMap<>();
|
||||||
|
String signStr = "";
|
||||||
|
for (int i = 0; i < key.length; i++) {
|
||||||
|
requestDataMap.put(key[i], value[i]);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
signStr = (sign1(requestDataMap) + "&api_key=xccdb-power-bank").replaceFirst("&", "");
|
||||||
|
Log.i(TAG, "createRequestBody: " + sign1(requestDataMap));
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
Log.i(TAG, "createRequestBody: " + signStr);
|
||||||
|
for (String keySet : requestDataMap.keySet()) {
|
||||||
|
requestBodyMap.put(keySet, RequestBody.create(MediaType.parse("multipart/form-data"), requestDataMap.get(keySet) + ""));
|
||||||
|
}
|
||||||
|
Log.i(TAG, "createRequestBody: " + MD5Utils.md5((signStr)));
|
||||||
|
requestBodyMap.put("sign", RequestBody.create(MediaType.parse("multipart/form-data"),
|
||||||
|
MD5Utils.md5((signStr))));
|
||||||
|
requestBodyMap.put("time", RequestBody.create(MediaType.parse("multipart/form-data"), TimeUtil.getNowMills() / 1000 + ""));
|
||||||
|
return requestBodyMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String sign1(Map params) throws JSONException {
|
||||||
|
JSONObject header = new JSONObject(JsonUtils.getInstance().getGson().toJson(params));
|
||||||
|
//获取header元素集合
|
||||||
|
Iterator headerIt = header.keys();
|
||||||
|
List<String> list = new ArrayList<String>();
|
||||||
|
//把header元素集合迭代 出 "param=value"形式字符串放入list集合中
|
||||||
|
while (headerIt.hasNext()) {
|
||||||
|
String param = headerIt.next().toString();
|
||||||
|
String value = StringUtils.encode(header.getString(param));
|
||||||
|
list.add(param + "=" + value);
|
||||||
|
}
|
||||||
|
Collections.sort(list);
|
||||||
|
String paramStr = "";
|
||||||
|
//迭代list拼装签名sign
|
||||||
|
for (int i = 0; i < list.size(); i++) {
|
||||||
|
paramStr += "&" + list.get(i);
|
||||||
|
}
|
||||||
|
String signStr = paramStr;
|
||||||
|
return signStr;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
package com.oneclouds.cargo.request.net;
|
||||||
|
|
||||||
|
|
||||||
|
import com.dahe.mylibrary.CommonBaseLibrary;
|
||||||
|
import com.dahe.mylibrary.net.JsonInterceptor;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
||||||
|
import io.reactivex.rxjava3.core.Observable;
|
||||||
|
import io.reactivex.rxjava3.core.ObservableSource;
|
||||||
|
import io.reactivex.rxjava3.core.ObservableTransformer;
|
||||||
|
import io.reactivex.rxjava3.schedulers.Schedulers;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import retrofit2.Converter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Administrator on 2018/8/8 0008.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class DataManager {
|
||||||
|
private volatile static DataManager mInstance;
|
||||||
|
|
||||||
|
public DataManager() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> ObservableTransformer<T, T> setThread() {
|
||||||
|
return new ObservableTransformer<T, T>() {
|
||||||
|
@Override
|
||||||
|
public ObservableSource<T> apply(Observable<T> upstream) {
|
||||||
|
return upstream.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Api getInstance() {
|
||||||
|
if (null == mInstance) {
|
||||||
|
synchronized (DataManager.class) {
|
||||||
|
if (null == mInstance) {
|
||||||
|
mInstance = new DataManager();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return api();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Api api() {
|
||||||
|
return CommonBaseLibrary.getRetrofit().newBuilder()
|
||||||
|
.client(new OkHttpClient.Builder()
|
||||||
|
.connectTimeout(30, TimeUnit.SECONDS)
|
||||||
|
.readTimeout(30, TimeUnit.SECONDS)
|
||||||
|
.writeTimeout(30, TimeUnit.SECONDS)
|
||||||
|
.addInterceptor(new JsonInterceptor())
|
||||||
|
.addInterceptor(new RequestHeadInterceptor())
|
||||||
|
.build())
|
||||||
|
// .addNetworkInterceptor(new TokenInterceptor())
|
||||||
|
.build().create(Api.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,143 @@
|
|||||||
|
package com.oneclouds.cargo.request.net;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.TypeAdapter;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
import com.google.gson.stream.JsonReader;
|
||||||
|
import com.oneclouds.cargo.util.http.SecurityUtil;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.io.StringReader;
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
|
import okhttp3.MediaType;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
import okhttp3.ResponseBody;
|
||||||
|
import retrofit2.Converter;
|
||||||
|
import retrofit2.Retrofit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName JsonConverterFactory
|
||||||
|
* @Author 用户
|
||||||
|
* @Date 2022/8/30 17:58
|
||||||
|
* @Description TODO
|
||||||
|
*/
|
||||||
|
public class JsonConverterFactory extends Converter.Factory {
|
||||||
|
private static final String TAG = "JsonConverterFactory";
|
||||||
|
private final Gson gson;
|
||||||
|
|
||||||
|
public static JsonConverterFactory create() {
|
||||||
|
return create(new Gson());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static JsonConverterFactory create(Gson gson) {
|
||||||
|
return new JsonConverterFactory(gson);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private JsonConverterFactory(Gson gson) {
|
||||||
|
if (gson == null) throw new NullPointerException("gson == null");
|
||||||
|
this.gson = gson;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
|
||||||
|
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
|
||||||
|
return new JsonRequestBodyConverter<>(gson, adapter); //请求
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
|
||||||
|
|
||||||
|
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
|
||||||
|
//TypeAdapter<?> adapter = gson.getAdapter(new TypeToken<>(){}.getType());
|
||||||
|
return new JsonResponseBodyConverter<>(gson, adapter); //响应
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JsonRequestBodyConverter<T>
|
||||||
|
*
|
||||||
|
* @param <T>
|
||||||
|
*/
|
||||||
|
public static class JsonRequestBodyConverter<T> implements Converter<T, RequestBody> {
|
||||||
|
private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
|
||||||
|
private final Gson gson;
|
||||||
|
private final TypeAdapter<T> adapter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造器
|
||||||
|
*/
|
||||||
|
public JsonRequestBodyConverter(Gson gson, TypeAdapter<T> adapter) {
|
||||||
|
this.gson = gson;
|
||||||
|
this.adapter = adapter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RequestBody convert(T value) throws IOException {
|
||||||
|
|
||||||
|
//这里需要,特别注意的是,request是将T转换成json数据。
|
||||||
|
//你要在T转换成json之后再做加密。
|
||||||
|
//再将数据post给服务器,同时要注意,你的T到底指的那个对象 这个加密请求体 转换为用自己的加密方式
|
||||||
|
String byteDecrypt = SecurityUtil.encrypt(value.toString());
|
||||||
|
// Log.e("=====>", "request中传递的json数据:" + value.toString()); //打印:加密前的json字符串
|
||||||
|
// Log.e("=====>", "加密后的字节数组:" + byteDecrypt.toString());//打印:字节数组
|
||||||
|
//传入字节数组,创建RequestBody 对象
|
||||||
|
return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), byteDecrypt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JsonResponseBodyConverter<T>
|
||||||
|
*
|
||||||
|
* @param <T>
|
||||||
|
*/
|
||||||
|
public class JsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
|
||||||
|
private final Gson mGson;//gson对象
|
||||||
|
private final TypeAdapter<T> adapter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造器
|
||||||
|
*/
|
||||||
|
public JsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
|
||||||
|
this.mGson = gson;
|
||||||
|
this.adapter = adapter;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转换
|
||||||
|
*
|
||||||
|
* @param responseBody
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public T convert(ResponseBody responseBody) throws IOException {
|
||||||
|
|
||||||
|
|
||||||
|
// 如果想解密 在这里也可以解密 然后把数据传回去 我在返回数据那里解密
|
||||||
|
//这部分代码参考GsonConverterFactory中GsonResponseBodyConverter<T>的源码对json的处理
|
||||||
|
Reader reader = StringToReader(responseBody.string().toString().trim());
|
||||||
|
JsonReader jsonReader = gson.newJsonReader(reader);
|
||||||
|
try {
|
||||||
|
return adapter.read(jsonReader);
|
||||||
|
} finally {
|
||||||
|
reader.close();
|
||||||
|
jsonReader.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* String转Reader
|
||||||
|
*
|
||||||
|
* @param json
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private Reader StringToReader(String json) {
|
||||||
|
Reader reader = new StringReader(json);
|
||||||
|
return reader;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
package com.oneclouds.cargo.request.net;
|
||||||
|
|
||||||
|
import android.text.TextUtils;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.dahe.mylibrary.utils.AppUtils;
|
||||||
|
import com.dahe.mylibrary.utils.BaseUtils;
|
||||||
|
import com.oneclouds.cargo.ui.App;
|
||||||
|
import com.oneclouds.cargo.ui.LoginActivity;
|
||||||
|
import com.oneclouds.cargo.util.SPUtil;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import cn.hutool.crypto.SecureUtil;
|
||||||
|
import okhttp3.Interceptor;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 头部Interceptor
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class RequestHeadInterceptor implements Interceptor {
|
||||||
|
private static final String TAG = "RequestHeadInterceptor";
|
||||||
|
@Override
|
||||||
|
public Response intercept(Chain chain) throws IOException {
|
||||||
|
Request.Builder builder = chain.request().newBuilder();
|
||||||
|
if (!TextUtils.isEmpty(SPUtil.getSP(App.getApp(), LoginActivity.USER,LoginActivity.USER_TOKEN))) {
|
||||||
|
// builder.addHeader("Authorization", SPUtils.getUserInfo(App.getApp()).getToken());
|
||||||
|
builder.addHeader("Authorization", SPUtil.getSP(App.getApp(), LoginActivity.USER,LoginActivity.USER_TOKEN));
|
||||||
|
}
|
||||||
|
Request request = builder
|
||||||
|
.addHeader("versionCode", AppUtils.getAppVersionCode() + "")
|
||||||
|
.addHeader("versionValue", AppUtils.getAppVersionName() + "")
|
||||||
|
.addHeader("deviceType", "ANDROID")
|
||||||
|
.addHeader("language", BaseUtils.getLanguage(App.getApp())+"")
|
||||||
|
.build();
|
||||||
|
Log.i(TAG, "intercept: " + request.headers());
|
||||||
|
return chain.proceed(request);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,133 @@
|
|||||||
|
package com.oneclouds.cargo.request.net;
|
||||||
|
|
||||||
|
import android.accounts.NetworkErrorException;
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.app.ProgressDialog;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
import android.view.Window;
|
||||||
|
|
||||||
|
import com.dahe.mylibrary.net.CommonResponseBean;
|
||||||
|
import com.dahe.mylibrary.net.ResultException;
|
||||||
|
import com.dahe.mylibrary.utils.ToastUtils;
|
||||||
|
import com.oneclouds.cargo.ui.MainActivity;
|
||||||
|
import com.oneclouds.cargo.ui.UiAuxiliary;
|
||||||
|
|
||||||
|
import java.net.ConnectException;
|
||||||
|
import java.net.SocketTimeoutException;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
|
import javax.net.ssl.SSLException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 统一的网络回调
|
||||||
|
*/
|
||||||
|
|
||||||
|
public abstract class RxHttpCallBack<T> {
|
||||||
|
private ProgressDialog dialog;
|
||||||
|
|
||||||
|
private void initDialog(Activity activity, String dialogMessage) {
|
||||||
|
dialog = new ProgressDialog(activity);
|
||||||
|
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
|
dialog.setCanceledOnTouchOutside(false);
|
||||||
|
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
|
||||||
|
dialog.setMessage(dialogMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
public RxHttpCallBack(Activity activity, String dialogMessage) {
|
||||||
|
initDialog(activity, TextUtils.isEmpty(dialogMessage) ? "网络请求中......" : dialogMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
public RxHttpCallBack(Activity activity) {
|
||||||
|
initDialog(activity, "网络请求中......");
|
||||||
|
}
|
||||||
|
|
||||||
|
public RxHttpCallBack() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 订阅成功
|
||||||
|
*/
|
||||||
|
public void onStart() {
|
||||||
|
if (dialog != null && !dialog.isShowing()) {
|
||||||
|
dialog.show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onStop(){
|
||||||
|
if (dialog != null && dialog.isShowing()) {
|
||||||
|
dialog.dismiss();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回成功
|
||||||
|
*
|
||||||
|
* @param t
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public void onSuccess(CommonResponseBean<T> t) {
|
||||||
|
if (dialog != null && dialog.isShowing()) {
|
||||||
|
dialog.dismiss();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回成功了,但是code错误
|
||||||
|
*
|
||||||
|
* @param t
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public void onCodeError(Context mContext, CommonResponseBean<T> t) {
|
||||||
|
if (dialog != null && dialog.isShowing()) {
|
||||||
|
dialog.dismiss();
|
||||||
|
|
||||||
|
}
|
||||||
|
if (t.getCode() == 401) {
|
||||||
|
// UiAuxiliary.delLogin(mContext);
|
||||||
|
// Intent intent = new Intent(mContext, MainActivity.class);
|
||||||
|
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||||
|
// mContext.startActivity(intent);
|
||||||
|
// ToastUtils.showToast(mContext,"您的账号再异地登录,请重新登录");
|
||||||
|
}else{
|
||||||
|
ToastUtils.showToast(mContext,t.getMsg());
|
||||||
|
}
|
||||||
|
|
||||||
|
// if (t.getMessage().getMsg_code() == 100 && SPUtils.hasUserInfo(mContext)) {
|
||||||
|
// SPUtils.remove(mContext, SPUtils.USER_INFO_KEY);
|
||||||
|
// } else {
|
||||||
|
// ToastUtils.showToast(mContext, t.getMessage().getMsg_desc());
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回失败
|
||||||
|
*
|
||||||
|
* @param e
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public void onFailure(Context mContext, Throwable e) {
|
||||||
|
if (dialog != null && dialog.isShowing()) {
|
||||||
|
dialog.dismiss();
|
||||||
|
}
|
||||||
|
if (e instanceof ConnectException
|
||||||
|
|| e instanceof TimeoutException
|
||||||
|
|| e instanceof SocketTimeoutException
|
||||||
|
|| e instanceof SSLException
|
||||||
|
|| e instanceof NetworkErrorException
|
||||||
|
|| e instanceof UnknownHostException) {
|
||||||
|
ToastUtils.showToast(mContext, "请检查您的网络~");
|
||||||
|
} else if (e instanceof ResultException) {
|
||||||
|
e.printStackTrace();
|
||||||
|
//自定义的ResultException
|
||||||
|
//由于返回200,300返回格式不统一的问题,自定义GsonResponseBodyConverter凡是300的直接抛异常
|
||||||
|
// System.out.println("---------errorCode------->"+((ResultException) e).getErrCode());
|
||||||
|
}else if (e instanceof SSLException){
|
||||||
|
ToastUtils.showToast(mContext, "网络连接终端,请检查您的网络~");
|
||||||
|
}else{
|
||||||
|
ToastUtils.showToast(mContext, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
package com.oneclouds.cargo.request.net;
|
||||||
|
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import okhttp3.Interceptor;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
import okhttp3.ResponseBody;
|
||||||
|
import okio.Buffer;
|
||||||
|
import okio.BufferedSource;
|
||||||
|
|
||||||
|
public class TokenInterceptor implements Interceptor {
|
||||||
|
@Override
|
||||||
|
public Response intercept(Chain chain) throws IOException {
|
||||||
|
Request request = chain.request();
|
||||||
|
Response response = chain.proceed(request);
|
||||||
|
|
||||||
|
ResponseBody responseBody = response.body();
|
||||||
|
if (responseBody != null) {
|
||||||
|
BufferedSource source = responseBody.source();
|
||||||
|
source.request(Long.MAX_VALUE); // Buffer the entire body.
|
||||||
|
Buffer buffer = source.buffer();
|
||||||
|
try {
|
||||||
|
String result = buffer.clone().readString(StandardCharsets.UTF_8);
|
||||||
|
JSONObject jsonObject = new JSONObject(result);
|
||||||
|
int code = jsonObject.getInt("code");
|
||||||
|
if (code == -1) {//判断返回code
|
||||||
|
|
||||||
|
// SPUtils.cleanUserInfo(App.getApp());
|
||||||
|
// Intent intent = new Intent(App.getApp(), LoginActivity.class);
|
||||||
|
// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
|
// App.getApp().startActivity(intent);
|
||||||
|
// ToastUtils.showToast(App.getApp(), "您的账号再异地登录,请重新登录");
|
||||||
|
// ToastUtils.showShort("身份过期,请重新登录");
|
||||||
|
//跳转方式
|
||||||
|
//1.使用MyApplication添加跳转
|
||||||
|
//2.使用路由跳转
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,8 @@ import android.app.Application;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.ApplicationInfo;
|
import android.content.pm.ApplicationInfo;
|
||||||
|
|
||||||
|
import com.dahe.mylibrary.CommonBaseLibrary;
|
||||||
|
import com.oneclouds.cargo.request.net.Api;
|
||||||
import com.oneclouds.cargo.util.SPUtil;
|
import com.oneclouds.cargo.util.SPUtil;
|
||||||
import com.umeng.commonsdk.UMConfigure;
|
import com.umeng.commonsdk.UMConfigure;
|
||||||
|
|
||||||
@ -17,11 +19,14 @@ public class App extends Application {
|
|||||||
|
|
||||||
|
|
||||||
private Context con;
|
private Context con;
|
||||||
|
public static App mApplication = null;
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
|
mApplication = this;
|
||||||
//设置非debug版本开启友盟
|
//设置非debug版本开启友盟
|
||||||
con=getBaseContext();
|
con=getBaseContext();
|
||||||
|
CommonBaseLibrary.getInstance().init(this, Api.BASE_URL);
|
||||||
String state= SPUtil.getSP(con,"state_data","authorization_state");
|
String state= SPUtil.getSP(con,"state_data","authorization_state");
|
||||||
if("1".equals(state)){
|
if("1".equals(state)){
|
||||||
if(!isDebugVersion(getApplicationContext())){
|
if(!isDebugVersion(getApplicationContext())){
|
||||||
@ -46,6 +51,10 @@ public class App extends Application {
|
|||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static App getApp() {
|
||||||
|
return mApplication;
|
||||||
|
}
|
||||||
|
|
||||||
private static boolean isDebugVersion(Context context) {
|
private static boolean isDebugVersion(Context context) {
|
||||||
try {
|
try {
|
||||||
ApplicationInfo info = context.getApplicationInfo();
|
ApplicationInfo info = context.getApplicationInfo();
|
||||||
|
@ -1,70 +0,0 @@
|
|||||||
package com.oneclouds.cargo.ui.wallet;
|
|
||||||
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
|
|
||||||
import com.oneclouds.cargo.util.adapter.ManyBean;
|
|
||||||
import com.oneclouds.cargo.util.bean.GetObjectName;
|
|
||||||
import com.zhpan.bannerview.BaseBannerAdapter;
|
|
||||||
import com.zhpan.bannerview.BaseViewHolder;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author hlh
|
|
||||||
* @version 1.0.0
|
|
||||||
* @date 2021/8/19 11:03
|
|
||||||
* @description:
|
|
||||||
*/
|
|
||||||
public class BannerApdate extends BaseBannerAdapter<Object> {
|
|
||||||
|
|
||||||
private int layout;
|
|
||||||
private List<ManyBean> object;
|
|
||||||
private Context con;
|
|
||||||
// private List<BaseViewHolder<Object>> bvs=new ArrayList<>();
|
|
||||||
// public void initBvs(int size){
|
|
||||||
// for(int i=0;i<size;i++){
|
|
||||||
// bvs.add(null);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
public BannerApdate(Context con, int layout) {
|
|
||||||
this.layout = layout;
|
|
||||||
this.con=con;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void bindData(BaseViewHolder<Object> holder, Object data, int position, int pageSize){
|
|
||||||
View v=holder.itemView;
|
|
||||||
ViewGroup vg=v.findViewById(v.getId());
|
|
||||||
GetObjectName.ZIModel(vg,data,con);
|
|
||||||
listenterView.onItemView(position,object,holder, layout);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getLayoutId(int viewType) {
|
|
||||||
return layout;
|
|
||||||
}
|
|
||||||
|
|
||||||
// public View getItemView(int position){
|
|
||||||
// if(bvs.get(position)!=null){
|
|
||||||
// return bvs.get(position).itemView;
|
|
||||||
// }
|
|
||||||
// return null;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// public int getItemSize(){
|
|
||||||
// return bvs.size();
|
|
||||||
// }
|
|
||||||
// public List<BaseViewHolder<Object>> getBvs() {
|
|
||||||
// return bvs;
|
|
||||||
// }
|
|
||||||
|
|
||||||
private onItemViewListenter listenterView;
|
|
||||||
public void setOnItemViewListener(onItemViewListenter listenterView) { this.listenterView = listenterView; }
|
|
||||||
public interface onItemViewListenter { void onItemView(int position, Object o, BaseViewHolder<Object> v, int layout);}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user