zoukankan      html  css  js  c++  java
  • soap天气查询

    public class MainActivity extends AppCompatActivity {
        private TextView tvContent;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //找控件
            //局部变量变成员变量快捷键 : Ctrl + Alt + F  -> 敲回车(Enter)
            tvContent = (TextView) findViewById(R.id.tvContent);
        }
        public void btnGetWeather(View view){
            //Alter + Enter 导包快捷键
            new Thread(){
                @Override
                public void run() {
                    getWeather();
                }
            }.start();
        }
        /**
         * 获取天气
         */
        private void getWeather(){
            //异常捕获快捷键 : Ctrl + Alt + T
            try {
                //创建一个信封
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                //信纸  namespace : 命名空间  移动通信学院    name : 服务名字  天气服务  Android、测试、IOS
                SoapObject object = new SoapObject("http://WebXml.com.cn/","getWeather");
                //设置请求参数
                object.addProperty("theCityCode","北京");
                object.addProperty("theUserID","7b810fda62b04020b0f23e443e6e8338");
                //将信装到信封里面
                envelope.bodyOut = object;
                //设置跨语言平台兼容性
                envelope.dotNet = true;
               //呼叫快递员
                HttpTransportSE httpTransportSE = new HttpTransportSE("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx");
                //让快递员送信
                //soapAction : 动作 -> 邮票
                httpTransportSE.call("http://WebXml.com.cn/getWeather",envelope);
                //等待服务器响应
                if(envelope.getResponse() != null){//判断服务器响应内容是否为空,如果为空,则没有任何信息,反之亦然
                        //得到服务器响应的内容
                        final SoapObject result = (SoapObject) envelope.bodyIn;
                    System.out.println("请求结果 : "+result.toString());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //运行在主线程里面的
                            //设置文本信息
                            tvContent.setText(result.toString());
                        }
                    });
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    }
  • 相关阅读:
    spring注解之@PostConstruct在项目启动时执行指定方法
    maven profile动态选择配置文件
    使用Java High Level REST Client操作elasticsearch
    ElasticSearch的基本原理与用法
    Spring Boot中使用Swagger2自动构建API文档
    Spring Aop——给Advice传递参数
    一次EF批量插入多表数据的性能优化经历
    [翻译]:SQL死锁-死锁排除
    项目中死锁的解决经历
    [翻译]:SQL死锁-为什么会出现死锁
  • 原文地址:https://www.cnblogs.com/leshen/p/7422909.html
Copyright © 2011-2022 走看看