美文网首页
Android开发中处理Webservice的接口

Android开发中处理Webservice的接口

作者: 沿路旅程如歌蜕变 | 来源:发表于2017-03-31 14:21 被阅读90次

    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);`
    一切又回到了我喜欢样子.

    相关文章

      网友评论

          本文标题:Android开发中处理Webservice的接口

          本文链接:https://www.haomeiwen.com/subject/qfcvottx.html