应用目录结构
1.封装请求地址常量类
package com.fuxun.property.config;
import com.ylink.commons.entity.PosLoginInfoDto;
/**
* 全局常量
*
* @author twilight
* @Time 2019-07-21
*/
public class PropertyConstant {
/**
* 请求后台的所有API接口都在这里配置;统一管理
*/
public static class Server {
public final static String _4057 = "4057";
public final static String _4060 = "4060";
public final static String _4061 = "4061";
public final static String _4067 = "4067";
public final static String _4068 = "4068";
public final static String _4069 = "4069";
}
/**
* 全局静态变量
*/
public static class Param {
}
/**
* sp 常量
*/
public static class SP {
public static final String SP = "property_sp";//sp
}
/**
* 全局常量
*/
public static class FINALVALUE {
}
}
2.封装网络工具类
package com.fuxun.property.http;
import android.util.Base64;
import com.alibaba.fastjson.JSONObject;
import com.fuxun.property.config.PropertyConstant;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ylink.commons.config.CommonsConstant;
import com.ylink.commons.utils.JsonUtils;
import com.ylink.commons.utils.MD5;
import com.ylink.https.base.BaseVo;
import com.ylink.https.config.HttpsConstant;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import okhttp3.MediaType;
import okhttp3.RequestBody;
/**
* 工具类,把vo转换成map
*
* @author weishixiong
* @Time 2018-03-19
*/
public class HttpUtil {
/**
* vo转换为Map
*
* @param vo
* @return
*/
public static RequestBody convertVo2Json(BaseVo vo) {
Map maps = convertVo2Params(vo);
JSONObject object = new JSONObject(maps);
return RequestBody.create(MediaType.parse("Content-Type, application/json"),
"{\"data\":" + object.toString() + "}");
}
/**
* vo转换为Map
*
* @param vo
* @return
*/
public static Map<String, String> convertVo2Params(BaseVo vo) {
Gson gson = new Gson();
Type type = new TypeToken<Map<String, String>>() {
}.getType();
return gson.fromJson(gson.toJson(vo), type);
}
/**
* 多文件转换为 Map<String, RequestBody> bodyMap
* 多文件上传参考:https://www.jianshu.com/p/d4f77e7a4e00
* @param files
* @param mediaType 文件类型
* @return
*/
public static Map<String, RequestBody> convertVo2BodyMap(List<File> files, String mediaType) {
Map<String, RequestBody> bodyMap = new HashMap<>();
for (int i = 0; i < files.size(); i++) {
bodyMap.put("file" + i + "\"; filename=\"" + files.get(i).getName(), RequestBody.create(MediaType.parse(mediaType), files.get(i)));
}
return bodyMap;
}
/**
* 获取请求头
* @return
*/
public static Map<String, String> getHeaders(final Object bodyObj){
Map<String, String> headers = new HashMap<>();
headers.put(CommonsConstant.SP.AUTH, CommonsConstant.Param.TOKEN);
headers.put(CommonsConstant.SP.STORE_ID, CommonsConstant.Param.STORE_ID);
headers.put(CommonsConstant.SP.DEVICE_NUMBER, CommonsConstant.Param.IMEI);
headers.put("加密密钥key", getEncryptValue(JsonUtils.jsonFormat(bodyObj), "加密密钥value")); //报文加密值
return headers;
}
private static String getEncryptValue(String params, String token) {
String encryptValue;
if (params.length() > 0) {
encryptValue = JsonUtils.sortJson(params);
try {
encryptValue = Base64.encodeToString(encryptValue.getBytes("utf-8"), Base64.DEFAULT);
encryptValue = encryptValue.replaceAll("\r|\n", "");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
encryptValue = MD5.md5(encryptValue + token);
} else {
encryptValue = MD5.md5(token);
}
return encryptValue;
}
}
3.封装接口类
package com.fuxun.property.http;
import com.fuxun.property.config.PropertyConstant;
import com.fuxun.property.model.dto.PosRecvInfoSyncDto;
import com.fuxun.property.model.dto.RevokeRefundDto;
import com.fuxun.property.model.dto.RevokeRefundPreOrderDto;
import com.fuxun.property.model.dto.ScanCodePreOrderDto;
import com.fuxun.property.model.dto.TransManageListDto;
import com.fuxun.property.model.dto.TransRecordDetailDto;
import com.fuxun.property.model.vo.PosRecvInfoSyncVo;
import com.fuxun.property.model.vo.RevokeRefundPreOrderVo;
import com.fuxun.property.model.vo.RevokeRefundVo;
import com.fuxun.property.model.vo.ScanCodePreOrderVo;
import com.fuxun.property.model.vo.TransManageListVo;
import com.fuxun.property.model.vo.TransRecordDetailVo;
import com.ylink.https.base.BaseDto;
import java.util.Map;
import io.reactivex.Flowable;
import retrofit2.http.Body;
import retrofit2.http.HeaderMap;
import retrofit2.http.POST;
/**
* api接口
*
* @author twilight
* @Time 2019-07-21
*
* retrofit的注解学习https://blog.csdn.net/qiang_xi/article/details/53959437
*/
public interface ApiService {
/**
* 扫描订单码预下单
* @param headers
* @param scanCodePreOrderVo
* @return
*/
@POST(PropertyConstant.Server._4067)
Flowable<BaseDto<ScanCodePreOrderDto>> scanCodePreOrder(@HeaderMap Map<String, String> headers,
@Body ScanCodePreOrderVo scanCodePreOrderVo);
}
4.封装DTO类——即返回的数据封装模型
package com.fuxun.property.model.dto;
import java.io.Serializable;
/**
* 物业收款预下单返回实体类
*/
public class ScanCodePreOrderDto implements Serializable {
private String userNo; //用户编号
private String acctNo; //用户账号
private String prdCode; //商品编号
private String prdName; //商品信息
private String payCurrency; //币种
private String payAmount; //交易金额
private String terOrderNo; //平台订单号
private String merOrderNo; //商户订单号
public String getUserNo() {
return userNo;
}
public void setUserNo(String userNo) {
this.userNo = userNo;
}
public String getAcctNo() {
return acctNo;
}
public void setAcctNo(String acctNo) {
this.acctNo = acctNo;
}
public String getPrdCode() {
return prdCode;
}
public void setPrdCode(String prdCode) {
this.prdCode = prdCode;
}
public String getPrdName() {
return prdName;
}
public void setPrdName(String prdName) {
this.prdName = prdName;
}
public String getPayCurrency() {
return payCurrency;
}
public void setPayCurrency(String payCurrency) {
this.payCurrency = payCurrency;
}
public String getPayAmount() {
return payAmount;
}
public void setPayAmount(String payAmount) {
this.payAmount = payAmount;
}
public String getTerOrderNo() {
return terOrderNo;
}
public void setTerOrderNo(String terOrderNo) {
this.terOrderNo = terOrderNo;
}
public String getMerOrderNo() {
return merOrderNo;
}
public void setMerOrderNo(String merOrderNo) {
this.merOrderNo = merOrderNo;
}
}
5.封装请求的数据VO类
package com.fuxun.property.model.vo;
import com.ylink.https.base.BaseVo;
/**
* 物业收款预下单Vo
*/
public class ScanCodePreOrderVo extends BaseVo {
private String url; //业务订单生成URL
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
6.数据请求仓库(接口)
package com.fuxun.property.repository;
import android.arch.lifecycle.LiveData;
import com.fuxun.property.model.dto.ScanCodePreOrderDto;
import com.fuxun.property.model.vo.ScanCodePreOrderVo;
import com.ylink.https.base.BaseDto;
/**
* 订单码收款Repository
*/
public interface IOrdercodeRepository {
/**
* 扫描订单码预下单
* @param scanCodePreOrderVo
* @return
*/
LiveData<BaseDto<ScanCodePreOrderDto>> scanCodePreOrder(ScanCodePreOrderVo scanCodePreOrderVo);
}
6.数据请求仓库实现类
package com.fuxun.property.repository;
import android.arch.lifecycle.LiveData;
import com.fuxun.property.http.ApiService;
import com.fuxun.property.http.HttpUtil;
import com.fuxun.property.model.dto.ScanCodePreOrderDto;
import com.fuxun.property.model.vo.ScanCodePreOrderVo;
import com.ylink.https.base.BaseDto;
import com.ylink.https.base.BaseRepository;
import com.ylink.https.http.RequetRetrofit;
/**
* 订单码收款Repository
*/
public class OrdercodeRepository extends BaseRepository implements IOrdercodeRepository {
/**
* 扫描订单码预下单
* @param scanCodePreOrderVo
* @return
*/
@Override
public LiveData<BaseDto<ScanCodePreOrderDto>> scanCodePreOrder(ScanCodePreOrderVo scanCodePreOrderVo) {
return request(RequetRetrofit.getInstance(ApiService.class).scanCodePreOrder(HttpUtil.getHeaders(scanCodePreOrderVo), scanCodePreOrderVo)).get();
}
}
7.调用数据仓库的工具类
我把packge和类的导入删掉了.
/**
* 物业收款主页viewmodel
*/
public class HomeViewModel extends ViewModel {
private HomeActivity activity;
public void setActivity(HomeActivity activity){
this.activity = activity;
}
/**
* 扫描订单码预下单
* @param url
*/
public LiveData<BaseDto<ScanCodePreOrderDto>> scanCodePreOrder(final String url){
IOrdercodeRepository ordercodeRepository = new OrdercodeRepository();
ScanCodePreOrderVo scanCodePreOrderVo = new ScanCodePreOrderVo();
scanCodePreOrderVo.setUrl(url);
return ordercodeRepository.scanCodePreOrder(scanCodePreOrderVo);
}
}
7.调用数工具类的Activity
/**
* 物业收款主页面
*/
public class HomeActivity extends BasePosActivity {
private CustomDlg customDlg;
private HomeViewModel homeViewModel;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//注意这里初始化
homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
homeViewModel.setActivity(this);
}
/**
* 发送订单码预下单
* @param url
*/
private void scanCodePreOrder(final String url){
showLoading(true);
//下面是真正的调用
homeViewModel.scanCodePreOrder(url).observe(this, new Observer<BaseDto<ScanCodePreOrderDto>>() {
@Override
public void onChanged(@Nullable BaseDto<ScanCodePreOrderDto> scanCodePreOrderDtoBaseDto) {
showLoading(false);
if(scanCodePreOrderDtoBaseDto.getStatusCode().equals(HttpsConstant.RespCode.R000)){
Log.d("log", "ScanCodePreOrderDto="+ JSONObject.toJSONString(scanCodePreOrderDtoBaseDto.getData()));
Intent intent = new Intent(HomeActivity.this, ScanOrdercodeRecvConActivity.class);
intent.putExtra("scanCodePreOrderDto",scanCodePreOrderDtoBaseDto.getData());
startActivity(intent);
}else{
customDlg = new CustomDlg(HomeActivity.this);
customDlg.show();
customDlg.setContent(scanCodePreOrderDtoBaseDto.getStatusDesc());
customDlg.setCancelTxt("取消");
customDlg.setConfirmTxt("重新尝试");
customDlg.addConfirmCallbackListener(new CustomDlg.ConfirmCallbackListener() {
@Override
public void onConfirm() {
onOrderCodeRecv(null); //重新扫码
}
});
customDlg.addCancelCallbackListener(new CustomDlg.CancelCallbackListener() {
@Override
public void onCancel() {
}
});
}
}
});
}
}
网友评论