Android学习之——(4)项目中的调用WebService学习


下面给出如何调用WebService的方法的处理:
public void DoTuiKuDJ(final String dtZzjl, final String dtRkdmx,
        final String dtRkd) {//这里的三个String都是将类放到List当中,然后将List转化成XML的形式来传递的,List转化XML的代码下面会给出
    new Webservice(context, handler, "TuiKuDJ", true, null, null, null) {
        @Override
        public void addProgerty(SoapObject rpc) {
                    // TODO Auto-generated method stub
            method = 2;
            rpc.addProperty("dtZzjl", dtZzjl);
            rpc.addProperty("dtRkdmx", dtRkdmx);
            rpc.addProperty("dtRkd", dtRkd);
        }
    };
}
/**
* 数据传回来处理
*/
private Handler handler = new Handler() {
    @SuppressWarnings("unchecked")
    public void handleMessage(android.os.Message msg) {
        switch (msg.what) {
        case 0:
            if (method == 1) {
                zzjl = (List<QiXieZzjl>) (msg.obj);//msg.obj为传回来的数据
                tuiKuFragment.DealZzjl();
                break;
            }
                break;
            break;
        }
        };
};
WebService类:
/**
 * web service通信类
 * @author Administrator 上传时要分int,String 而接受数据射计类都要是String
 */
public abstract class Webservice {
    public static final String NAMESPACE = "http://tempuri.org/";
    // WebService地址
    private String URL;
    /**
     * web service通信
     * @param context
     * @param handler
     * @param methodName
     *            方法名
     * @param flag
     *            true:get data that should be resolved ;false:send data
     * @param cla
     *            The class to be reflected
     * @param entityname
     *            需要转换成类的包名string
     */
    @SuppressWarnings("rawtypes")
    public Webservice(Context context, Handler handler, String methodName,
            boolean flag, Class cla, ParseData parse, String entityname) {
        URL = "http://"
                + context.getSharedPreferences("ServiceIP",
                        Context.MODE_PRIVATE).getString("IP",
                        "127.0.0.1/EOIISService.asmx");
        tongXun(context, handler, methodName, flag, cla, parse, entityname);
    }
    /**
     * 以webservice获取数据
     */
    @SuppressWarnings("rawtypes")
    private void tongXun(final Context context, final Handler handler,
            final String methodName, final boolean flag, final Class cla,
            final ParseData parse, final String entityname) {
        Thread thread = new Thread() {
            public void run() {
                webServiceThread(context, handler, methodName, flag, cla,
                        parse, entityname);
            }
        };
        thread.start();
        // thread.
    }
    /**
     * 设置webservice要传递的属性
     */
    public abstract void addProgerty(SoapObject rpc);
    /**
     * webservice 线程
     * 
     * @param context
     * @param handler
     * @param methodName
     * @param flag
     *            true:get data that should be resolved ;false:send data
     * @param cla
     *            The class to be reflected * @param entityname 需要转换成类的包名string
     */
    @SuppressWarnings("rawtypes")
    public void webServiceThread(final Context context, final Handler handler,
            String methodName, boolean flag, Class cla, final ParseData parse,
            String entityname) {
        try {
            // System.out.println("rpc------");
            // 指定webservice的命名空间和调用的方法名
            Object obj1 = null;
            SoapObject rpc = new SoapObject(NAMESPACE, methodName);
            addProgerty(rpc);
            MyHttptran ht = new MyHttptran(URL, 5000);
            ht.debug = true;
            // 生成调用Webservice方法的SOAP请求信息.该信息由SoapSerializationEnvelope对象描述
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.bodyOut = rpc;
            envelope.dotNet = true;
            envelope.setOutputSoapObject(rpc);
            ht.call(NAMESPACE + methodName, envelope);
            if (envelope.bodyIn.toString().startsWith("SoapFault")) {
                SoapFault sf = (SoapFault) (envelope.bodyIn);
                Log.i("fault", sf.faultstring);
                if (handler != null) {
                    Message message = Message.obtain(handler);
                    message.what = 3;
                    message.obj = sf.faultstring;
                    message.sendToTarget();
                }
                return;
            }
            // 得到返回结果
            Log.i("bodyIn", envelope.bodyIn.toString());
            SoapObject result = (SoapObject) envelope.bodyIn;
            if (result.toString().equals(methodName + "Response{}")) {
                // pd = false;
                if (null != handler) {
                    // send message to the main thread
                    Message message = Message.obtain(handler);
                    // message.obj = year;
                    message.what = 1;
                    message.sendToTarget();
                }
                return;
            }
            if (parse == null) {
                if (null != handler) {
                    Message message = Message.obtain(handler);
                    message.obj = result.getProperty(methodName + "Result")
                            .toString();
                    message.what = 1;
                    message.sendToTarget();
                }
                return;
            }
            SoapObject detail = (SoapObject) result.getProperty(methodName
                    + "Result");
            if (detail.toString().equals("anyType{}")) {
                if (null != handler) {
                    Message message = Message.obtain(handler);
                    message.what = 2;
                    message.sendToTarget();
                }
            } else {
                obj1 = parse.doing(detail, cla, entityname);
                // 发送消息回主线程
                if (null != handler) {
                    Message message = Message.obtain(handler);
                    message.obj = obj1;
                    message.what = 0;
                    message.sendToTarget();
                }
            }
        } catch (ConnectException e) {
            e.printStackTrace();
            if (null != handler) {
                handler.sendEmptyMessage(-1);
            }
        } catch (SocketTimeoutException e) {
            // TODO: handle exception
            e.printStackTrace();
            if (null != handler) {
                handler.sendEmptyMessage(-1);
            }
        } catch (XmlPullParserException e) {
            // TODO: handle exception
            e.printStackTrace();
            if (null != handler) {
                handler.sendEmptyMessage(-1);
            }
 
        } catch (Exception e) {
            e.printStackTrace();
            if (null != handler) {
                Message message = Message.obtain(handler);
                message.obj = e.getMessage();
                message.what = -2;
                message.sendToTarget();
            }
        }
    }
}
关于将List<Object> 转化为XML的方法
/**
 * 用于制造xml类型的字符串
 * @author Administrator syc
 * @since 2012-10-31
 */
public class CreateXml {
    public String createXml(@SuppressWarnings("rawtypes") List list,
            String tableName) {
        String xml = "";
        StringWriter xmlWriter = new StringWriter();
        XmlSerializer xmlSerializer = Xml.newSerializer();
        try {
            xmlSerializer.setOutput(xmlWriter);
            xmlSerializer.startDocument("UTF-16", null);
            xmlSerializer.startTag("", "NewDataSet");
            // DT
            if (null != list) {
                FanShe reflect = new FanShe();
                List<String> shuxingList = reflect.getPropertyNames(list.get(0)
                        .getClass());
                for (Object object : list) {
                    xmlSerializer.startTag("", object.getClass().getName());
                    @SuppressWarnings("unchecked")
                    List<String> vlaueList = reflect.getValueList(shuxingList,
                            object);
                    for (int i = 0; i < shuxingList.size(); i++) {
                        xmlSerializer.startTag("", shuxingList.get(i));
                        if (vlaueList.get(i) == null) {
                            xmlSerializer.text("");
                        } else {
                            xmlSerializer.text(vlaueList.get(i));
                        }
                        xmlSerializer.endTag("", shuxingList.get(i));
                    }
                    xmlSerializer.endTag("", object.getClass().getName());
                }
                xmlSerializer.endTag("", "NewDataSet");
                // 属性集合
                xmlSerializer.endDocument();
                xmlSerializer.flush();
                // xmlWriter.flush();
                xml = xmlWriter.toString().split("\\?>")[1];
            }
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (xmlWriter != null) {
                try {
                    xmlWriter.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return xml;
    }
}
转化为XML调用方式如下:
qiXieZzjlEntitieString = new CreateXml().createXml(qiXieZzjlEntities,"qiXieZzjlEntities");
//qiXieZzjlEntities声明如下:private List<QiXieZzjlEntity> qiXieZzjlEntities = new ArrayList<QiXieZzjlEntity>();     
//"qiXieZzjlEntities"字符串名字可以任意 



Mr.傅:学习笔记
欢迎转载,转载注明出处,谢谢


没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试