WebService调用网络查询天气

来源:互联网 发布:北交大淘宝地址怎么填 编辑:程序博客网 时间:2024/06/05 07:03

public void butt(View view){
  //网络请求时一个耗时操作,不能再主线程进行,放到子线程进行
       new Thread(){
           @Override
           public void run() {
               super.run();
               getWeather();
           }
       }.start();
    }

   
    public voidgetWeather(){
       try {
           //创建一个信封
           SoapSerializationEnvelope envelope = newSoapSerializationEnvelope(SoapEnvelope.VER11);
           //得到一张信纸
           SoapObject object = new SoapObject("http://WebXml.com.cn/","getMobileCodeInfo");
           //添加请求的参数
           object.addProperty("mobileCode","18513200461");
           object.addProperty("userID","94c18771ac054d57ac03997e4311f83e");
           //将信装到信纸
           envelope.bodyOut = object;
           //平台兼容性
           envelope.dotNet = true;
           //发送信封
           HttpTransportSE httpTransportSE = new HttpTransportSE("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx");
           httpTransportSE.call("http://WebXml.com.cn/getMobileCodeInfo",envelope);
           //等待对方回信
           if(envelope.getResponse() !=null){
               //得到信中的内容
               final SoapObject result = (SoapObject) envelope.bodyIn;
               System.out.println(result.toString());
               runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
      //主线程
                       weather.setText(result.toString());
                   }
               });
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
    }

原创粉丝点击