Android开发中处理Webservice的接口

SoapObject解析成List<String[]>二维数组

最近和Webservice对接接口,接到需求我乃一脸懵逼啊,废话不多说,看代码

首先是服务器接口给的url和方法名

private static final String TRANS_URL = "http://192.168.0.16:8005/WebService.asmx";
private static final String METHED = "DownLoadDataByFloor";
private static final String NAMESPACE = "http://tempuri.org/";//默认

NAMESPACE 可以在URL后面加上?wsdl既可以阅览,如下图

Paste_Image.png

继续撸代码

    private void getMesInfo(final String FloorName, String StepName, final String CurPage, final String PageSize) {

        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 100) {
                    if (dialogLoading != null) {
                        dialogLoading.closeDialog();
                        dialogLoading = null;
                    }
                    springView.onFinishFreshAndLoad();
                    springView.onFinishFreshAndLoad();
                    no_choose_tv.setVisibility(View.GONE);
                    SoapObject result = (SoapObject) msg.obj;
                    list.addAll(getSoapObj(result));
                    adapter.notifyDataSetChanged();
                }else if (msg.what==101){
                    if (dialogLoading != null) {
                        dialogLoading.closeDialog();
                        dialogLoading = null;
                    }
                    springView.onFinishFreshAndLoad();
                    springView.onFinishFreshAndLoad();
                    no_choose_tv.setVisibility(View.GONE);
                    MyUtil.showAutoToast(context,"暂无更多数据");
                    if (list.size()==0){
                        no_choose_tv.setVisibility(View.VISIBLE);
                        no_choose_tv.setText("暂无更多数据");
                    }
                }
            }
        };
        Log.e("开始连接服务器", FloorName + StepName + "第" + CurPage + "页");
        final SoapObject request = new SoapObject(NAMESPACE, METHED);
        request.addProperty("FloorName", FloorName);
        request.addProperty("StepName", StepName);
        request.addProperty("CurPage", CurPage);
        request.addProperty("PageSize", PageSize);
//      request.addProperty("MaxPage", "3");
        final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut = request;
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        if (CurPage.equals("1")) {
            dialogLoading = new DialogLoading(context, "加载中...");
            list.clear();
            adapter.notifyDataSetChanged();
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpTransportSE ht = new HttpTransportSE(TRANS_URL);
                ht.debug = true;
                try {
                    ht.call(soapAction, envelope);
                    SoapObject object = (SoapObject) envelope.bodyIn;
                    Message msg = new Message();
                    if (page>1&&((SoapObject)object.getProperty(0)).getPropertyCount()<1){
                        msg.what = 101;
                        page--;
                    }else{
                        msg.what = 100;
                        msg.obj = object;
                    }
                    handler.sendMessage(msg);
                    Log.e("getMesInfo", object.toString());
                } catch (Exception e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            MyUtil.showAutoToast(context,"连接超时");
                            no_choose_tv.setVisibility(View.VISIBLE);
                            no_choose_tv.setText("连接超时\n请确保您的网络为公司内网\n点击重新加载");
                        }
                    });
                    if (dialogLoading != null) {
                        dialogLoading.closeDialog();
                        dialogLoading = null;
                    }
                    Log.e("getMesInfo", e.getMessage().toString());
                    e.printStackTrace();
                }
            }
        }).start();
    }

这是笔者封装的网络请求,新开线程执行网络请求部分,然后在到handler处理更新listview和ui,其中

 list.addAll(getSoapObj(result));```
中的`getSoapObj()`方法如下,花了一天时间才弄明白

private List<String[]> getSoapObj(SoapObject object){
String[] string = null;
List<String []> aaa=new ArrayList<>();
if (((SoapObject)object.getProperty(0)).getPropertyCount()>0){
for (int i=0;i<((SoapObject)object.getProperty(0)).getPropertyCount();i++){
string=new String[((SoapObject) ((SoapObject) object.getProperty(0)).getProperty(0)).getPropertyCount()];
for (int j=0;j< ((SoapObject) ((SoapObject) object.getProperty(0)).getProperty(i)).getPropertyCount();j++){
string[j]=((SoapObject) ((SoapObject) object.getProperty(0)).getProperty(i)).getProperty(j).toString();
}
if (string!=null){
aaa.add(i,string);
}
}
}else {
if (page==1){
no_choose_tv.setVisibility(View.VISIBLE);
no_choose_tv.setText("暂无更多数据");
}
}
return aaa;
}

####看着代码很冗长,其实SoapObject相当于数组,只是不能直接当成数组用,所以我们要把它的数据取出来自己弄到数组中去,在源码中我找到一个超级好用的方法
在SoapObject的源码中

public int getPropertyCount() {
return this.properties.size();
}

这个方法等于list.size().也就是放回SoapObject对象中当前层的长度
有了这个方法,就能对着获得的SoapObject对象toString()之后研究它的模型构造.比如

DownLoadDataByFloorResponse{DownLoadDataByFloorResult=anyType{ArrayOfString=anyType{string=4F DIP C线; string=DIP4F_C; string=TW.C.ITV050-G; string=4.002.1188; string=999; string=999; string=0; string=0.00%; }; ArrayOfString=anyType{string=4F DIP C线; string=DIP4F_C; string=TW.C.ITV050-J; string=4.002.1367; string=100; string=100; string=2; string=2.00%; }; ArrayOfString=anyType{string=4F DIP D线; string=DIP4F_D; string=TW.C.ITV041-H; string=4.002.1068; string=1772; string=1980; string=7; string=0.35%; }; }; MaxPage=2; }

接口人员说给我返回的List<String[]>,所以开始解析时我一直以为一定是这样,其实不然,已经发生了翻天覆地的变化.
然后就是一层一层的解析
`object.getProperty(0)`之后发现还是不能直接循环取数据,所以只能把`object.getProperty(0)`之后的对象继续转成SaopObject继续`getProperty()`,
由于WebService那边返回的确实是List<String[]>,所以Android这边在第二次`getProperty()`就能开始循环了,循环的对象就是List,第三次`getProperty()`后的循环就可以直接添加到String[]里面去了.大工搞成.
`listview.setAdapter(context,list);`
一切又回到了我喜欢样子.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,767评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,973评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,460评论 25 708
  • (一) 烟一根一根的抽,每人都有寂寞的时候,患者真的是这样,通讯录人...
    烟火里的尘埃a阅读 308评论 1 5
  • 不的不承认看见那个熟悉的头像跳出来那个时候的心情是多么激动 以至于该怎么回答输入删除输入删除了好几遍 但是 也就像...
    白羊鱼啊阅读 316评论 0 0