博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android实现解析webservices
阅读量:6088 次
发布时间:2019-06-20

本文共 3881 字,大约阅读时间需要 12 分钟。

package com.example.ksoap2demo;import java.io.UnsupportedEncodingException;import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.SoapObject;import org.ksoap2.serialization.SoapSerializationEnvelope;import org.ksoap2.transport.HttpTransportSE;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {    private Button okButton;        // 命名空间    private static final String NAMESPACE = "http://WebXml.com.cn/";        // WebService地址    private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";    // 方法    private static final String METHOD_NAME = "getWeatherbyCityName";    /**     *  SOAPAction: "http://WebXml.com.cn/getWeatherbyCityName"     */    private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";    private String weatherToday;    private SoapObject detail;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // 查询按钮        okButton = (Button) this.findViewById(R.id.btn_Search);        okButton.setOnClickListener(new OnClickListener() {            public void onClick(View v) {                // TODO Auto-generated method stub                String city = "北京";                getWeather(city);            }        });    }    public void getWeather(final String cityName) {        new Thread(new Runnable() {            public void run() {                try {                    System.out.println("rpc------");                    SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);                    System.out.println("rpc..." + rpc);                    System.out.println("cityName is... " + cityName);                    rpc.addProperty("theCityName", cityName);                                        /**                     * 生成调用Webservice方法的SOAP请求信息                     *                      * 创建SoapSerializationEnvelope对象时需要通过SoapSerializationEnvelope类的构造方法设置SOAP协议的版本号                     * 。 该版本号需要根据服务端WebService的版本号设置。                     * 在创建SoapSerializationEnvelope对象后                     * ,不要忘了设置SOAPSoapSerializationEnvelope类的bodyOut属性,                     * 该属性的值就是在第一步创建的SoapObject对象。                     */                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(                            SoapEnvelope.VER11);                    envelope.bodyOut = rpc;                    envelope.dotNet = true;                    envelope.setOutputSoapObject(rpc);                    HttpTransportSE ht = new HttpTransportSE(URL);                    ht.debug = true;                    ht.call(SOAP_ACTION, envelope);                    detail = (SoapObject) envelope.getResponse();                    parseWeather(detail);                    return;                } catch (Exception e) {                    e.printStackTrace();                }            }        }).start();    }    private void parseWeather(SoapObject detail)            throws UnsupportedEncodingException {        String date = detail.getProperty(6).toString();        weatherToday = "今天:" + date.split(" ")[0];        weatherToday = weatherToday + "\n天气:" + date.split(" ")[1];        weatherToday = weatherToday + "\n气温:"                + detail.getProperty(5).toString();        weatherToday = weatherToday + "\n风力:"                + detail.getProperty(7).toString() + "\n";        System.out.println("weatherToday is " + weatherToday);        getWindow().getDecorView().post(new Runnable() {            public void run() {                Toast.makeText(getWindow().getDecorView().getContext(),                        weatherToday, Toast.LENGTH_LONG).show();            }        });    }}

JAR包下载:

转载地址:http://mppwa.baihongyu.com/

你可能感兴趣的文章
Spring+Shiro搭建基于Redis的分布式权限系统(有实例)
查看>>
Struts报java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory的解决办法
查看>>
java.util.concurrent.CyclicBarrier组件说明
查看>>
JS控制input只允许输入的各种指定内容
查看>>
Jsoup解析HTML
查看>>
mybatis(五)TypeHandler简介及配置(mybatis源码篇)
查看>>
Python 基础——字符串maketrans(),translate()
查看>>
MyEclipse下CVS的配置
查看>>
Node.js https.server API解析
查看>>
PHPCMS自定义标签,获取全部栏目的最新内容
查看>>
underscore: 竞价算法手记
查看>>
WebQQ更新:用户注册功能
查看>>
怎样才能在MathType中快速引用公式编号
查看>>
_BSMachError: (os/kern) invalid name (15)
查看>>
IDEA14.1 console log4j utf-8乱码
查看>>
SQL使用case when 动态的修改where条件
查看>>
[leetcode] Longest Valid Parentheses
查看>>
fork与Vfork比较
查看>>
java内存管理机制及内存区域详解
查看>>
Java 将PDF转图片上传到FTP上
查看>>