Fastlib网络模块 soap协议解决方案

并不是所有的接口都是按照常规的来的。

比如前段时间我公司接了一个三期的一个项目,项目内使用一个二封过的Volley。我想将自己的库移入来替代掉,但是发现这个项目使用的接口协议不太一样,我分析了一下做了个简单的二封。解决特殊协议的问题

首先有几个要点

  1. 使用soap协议
  2. 接口发送的是原始流数据
  3. xml格式发送和返回

1.协议就使用项目中的老代码已有的格式

soap协议具体是什么可以自行百度。现在我们仅关心如何发送和接收数据
这个项目已经写好了协议的其它项。cxf代表具体接口地址,requestParam是发送的参数。之后我们使用String.format(soap,cxf,requestParam,cxf);替换即可
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cxf="http://cxf.spring.core.sinosoft.com/">
    <soapenv:Header/>
    <soapenv:Body>
        <cxf:%s>
            <requestParam>%s</requestParam>
        </cxf:%s>
    </soapenv:Body>
</soapenv:Envelope>

2.使用继承复写发送网络请求前的方法来发送原始流数据

fastlib网络功能中有发送原始字节流的功能
Request.setByteStream(byte[]);
但是我们不直接使用,因为要发送的参数个具体接口要填充到协议中然后再转换成字节组发出去,所以使用继承来将这些重复的功能封一下
public class AppRequest extends Request{
    public static final String URL ="https://xxx/xxxx?wsdl";
    public static final String AOSPFORMAT = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
            "xmlns:cxf=\"http://cxf.spring.core.sinosoft.com/\">\n"+
            "<soapenv:Header/>\n" +
            "<soapenv:Body>\n" +
            "<cxf:%s>\n" +
            "<requestParam>%s</requestParam>\n" +
            "</cxf:%s>\n" +
            "</soapenv:Body>\n" +
            "</soapenv:Envelope>";

    public String mMethod;

    public AppRequest(String method){
        super("POST",URL);
        mMethod=method;
    }

    @Override
    public Request start(boolean forceRefresh) {
        Gson gson=new Gson();
        String content=gson.toJson(getParams()); //参数是put进参数的本体
        String realContent=String.format(AOSPFORMAT,mMethod,content,mMethod);

        setByteStream(realContent.getBytes()); //设置要发送的流数据
        return super.start(forceRefresh);
    }
}
这样封装好后用起来和Request一样,还是原来的味道,还是一样的配方

3.xml的返回使用全局来进行解析

接口的数据返回也是xml格式的。我们仅取return中的数据即可
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns2:expenseListAccBankListResponse xmlns:ns2="http://cxf.spring.core.sinosoft.com/">
            <return>{"success":"false","msg":"密匙不匹配!","serviceName":"expenseListAccBankList","obj":[]}</return>
        </ns2:expenseListAccBankListResponse>
    </soap:Body>
</soap:Envelope>
使用全局返回监听.即所有网络请求返回都会过一遍这个监听并且可以改变返回的数据,那么我们只要把return中的数据拆出来即可
NetManager.getInstance().setGlobalListener(new GlobalListener(){
    @Override
    public String onTranslateJson(Request r, String json){
        String contentStr=null;
        try {
            contentStr=parseSoapXml();
        } catch (XmlPullParserException | IOException e) {
            e.printStackTrace();
        }
        if(contentStr==null)
            onErrorListener(r,"解析xml失败");
        return contentStr;
    }
});

自此,可以按照约定的协议来使用fastlib的网络功能了
AppRequest request=new AppRequest("getBankCardList");
request.put("userId","1234");
request.setListener(new SimpleListener<ResponseBean<BankCard>>(){

    @Override
    public void onResponseListener(Request r, ResponseBean<BankCard> result){
        System.out.println(result);
    }
});
net(request);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值