android-基于Webservice实现天气信息获取及源码下载(二)

本项目说明如下:
1、分别使用三种方式获取服务信息。soap、http get、http post三种方式实现信息的获取。
2、基于android调用webservice服务详细步骤实现
博文链接上一篇地址:
android-基于Webservice实现天气信息获取及源码下载(一)
android-基于Webservice实现天气信息获取及源码下载(三)
本篇博文详细讲述开发过程。
首先看获取webservice信息的工具类代码:

public class WebServiceUtil {
    private static String TAG = "WebServiceUtil.class";
    private CallBack callBack;
    private WeatherCallBack weatherCallBack;
    /**
     * 获取省份的方法
     * @return
     */
    private void getProviceList(){
        final String method = "getRegionProvince";//该方法名不是随便写的,而是根据网站提供的接口规定的
        final HttpTransportSE httpTransportSE = new HttpTransportSE(Constants.URI);
        httpTransportSE.debug = true;
        final SoapSerializationEnvelope envelope =
                new SoapSerializationEnvelope(SoapEnvelope.VER11);//soap版本 这个基本随便 不一定使用ver11
        SoapObject soapObject = new SoapObject(Constants.URI_SERVER, method);
        //下面定义了三个参数,这三个也不是随便写的,而是根据接口规定的。
        soapObject.addProperty("Content-Type", "text/xml; charset=utf-8");
        soapObject.addProperty("SOAPAction",Constants.URI_SERVER+method);
        soapObject.addProperty("Content-Length", soapObject.toString().length());
        envelope.bodyOut = soapObject;
        envelope.dotNet = true;

        Log.i(TAG, "执行task");
        FutureTask<List<String>> task = new FutureTask<>(new Callable<List<String>>() {
            @Override
            public List<String> call() throws Exception {
                httpTransportSE.call(Constants.URI_SERVER + method , envelope);
                if (envelope.getResponse() != null) {
                    SoapObject result = (SoapObject) envelope.bodyIn;
                    //method+"Result" 这个字符串也不是随便写的,而是根据接口返回值规定的
                    SoapObject detail = (SoapObject) result.getProperty(method+"Result");
                    Log.i(TAG, detail.toString());
                    //通过回调 返回数据
                    callBack.getData((ArrayList<String>)parseProviceOrCity(detail));
                    return parseProviceOrCity(detail);
                }
                return null;
            }
        });
        new Thread(task).start();//开启线程,执行任务
        try {
            task.get();//获取数据
        } catch (Exception e) {
            e.printStackTrace();
            Log.i(TAG, "出现异常");
        }
    }

    private void GetCityList(String theRegionCode){
        final String method = "getSupportCityString";//该方法名不是随便写的,而是根据网站提供的接口规定的
        final HttpTransportSE httpTransportSE = new HttpTransportSE(Constants.URI);
        httpTransportSE.debug = true;
        final SoapSerializationEnvelope envelope =
                new SoapSerializationEnvelope(SoapEnvelope.VER11);
        SoapObject soapObject = new SoapObject(Constants.URI_SERVER, method);
        //下面定义了四个参数,这四个也不是随便写的,而是根据接口规定的。
        soapObject.addProperty("Content-Type", "text/xml; charset=utf-8");
        soapObject.addProperty("SOAPAction",Constants.URI_SERVER+method);
        soapObject.addProperty("theRegionCode", theRegionCode);
        soapObject.addProperty("Content-Length", soapObject.toString().length());
        envelope.bodyOut = soapObject;
        envelope.dotNet = true;
        FutureTask<List<String>> task = new FutureTask<>(new Callable<List<String>>() {
            @Override
            public List<String> call() throws Exception {
                httpTransportSE.call(Constants.URI_SERVER + method , envelope);
                if (envelope.getResponse() != null) {
                    SoapObject result = (SoapObject) envelope.bodyIn;
                    //method+"Result" 这个字符串也不是随便写的,而是根据接口返回值规定的
                    SoapObject detail = (SoapObject) result.getProperty(method+"Result");
                    Log.i(TAG, detail.toString());
                    //通过回调 返回数据
                    callBack.getData((ArrayList<String>)parseProviceOrCity(detail));
                    return parseProviceOrCity(detail);
                }
                return null;
            }
        });
        new Thread(task).start();
        try {
            task.get();
        } catch (Exception e) {
            e.printStackTrace();
            Log.i(TAG, "出现异常");
        }
    }
    private void GetWeather(String theCityCode){
        final String method = "getWeather";//该方法名不是随便写的,而是根据网站提供的接口规定的
        final HttpTransportSE httpTransportSE = new HttpTransportSE(Constants.URI);
        httpTransportSE.debug = true;
        final SoapSerializationEnvelope envelope =
                new SoapSerializationEnvelope(SoapEnvelope.VER11);
        SoapObject soapObject = new SoapObject(Constants.URI_SERVER, method);
        //下面定义了五个参数,这五个也不是随便写的,而是根据接口规定的。
        soapObject.addProperty("Content-Type", "text/xml; charset=utf-8");
        soapObject.addProperty("SOAPAction",Constants.URI_SERVER+method);
        soapObject.addProperty("theCityCode", theCityCode);
        soapObject.addProperty("theUserID", Constants.theUserID);
        soapObject.addProperty("Content-Length", soapObject.toString().length());
        envelope.bodyOut = soapObject;
        envelope.dotNet = true;
        FutureTask<List<String>> task = new FutureTask<>(new Callable<List<String>>() {
            @Override
            public List<String> call() throws Exception {
                httpTransportSE.call(Constants.URI_SERVER + method , envelope);
                if (envelope.getResponse() != null) {
                    SoapObject result = (SoapObject) envelope.bodyIn;
                    //method+"Result" 这个字符串也不是随便写的,而是根据接口返回值规定的
                    SoapObject detail = (SoapObject) result.getProperty(method+"Result");
                    Log.i(TAG, detail.toString());
                    //通过回调 返回数据
                    weatherCallBack.getData(detail.toString());
                    return parseProviceOrCity(detail);
                }
                return null;
            }
        });
        new Thread(task).start();
        try {
            task.get();
        } catch (Exception e) {
            e.printStackTrace();
            Log.i(TAG, "出现异常");
        }
    }

    //解析获取省份或者城市的数据的方法  解析非常简单
    private List<String> parseProviceOrCity(SoapObject soapObject){
        ArrayList<String> arrayList = new ArrayList<>();
        for (int i = 0; i < soapObject.getPropertyCount(); i++) {
            arrayList.add(soapObject.getProperty(i).toString().split(",")[0]);
        }
        return arrayList;
    }

    //获取省份回调
    public void setProviceCallBack(CallBack callBack){
        this.callBack = callBack;
        getProviceList();
    }
    //获取城市回调
    public void setCityCallBack(CallBack callBack,String theRegionCode){
        this.callBack = callBack;
        GetCityList(theRegionCode);
    }

    public void setWeatherCallBack(WeatherCallBack weatherCallBack,String theRedionCode){
        this.weatherCallBack = weatherCallBack;
        GetWeather(theRedionCode);
    }

    //定义回调接口获取数据
    public interface CallBack{
        public void getData(ArrayList<String> arrayList);
    }
    //定义回调接口获取数据
    public interface WeatherCallBack{
        public void getData(String weather);
    }
}

类中 使用了常量类,常量类代码如下:

public class Constants {
    //服务器地址
    public static String URI_SERVER ="http://WebXml.com.cn/";
    //提供服务的URI
    public static String URI = 
            "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";
    //注册用户的USERID  这个值换成你自己的值
    public static String theUserID = "Your userID";
}

WebServiceUtil类主要定义了三个方法分别获取省份、城市、天气,有了这三个方法基本就获取到了数据。
然后就是主界面-首先进入省份列表Activity,代码如下:

public class MainActivity extends Activity {
    private ProviceOrCityAdapter proviceAdapter;
    private final static String TAG = MainActivity.class.getSimpleName();
    private ArrayList<String> arrayList = new ArrayList<>();
    private TextView exitTx;
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        init();
        setClick();
        final WebServiceUtil webServiceUtil = new WebServiceUtil();
        webServiceUtil.setProviceCallBack(new CallBack() {
            @Override
            public void getData(ArrayList<String> arrayList) {
                MainActivity.this.arrayList = arrayList;
                Log.i(TAG, "执行");
                proviceAdapter = new ProviceOrCityAdapter(MainActivity.this, arrayList);
                listView.setAdapter(proviceAdapter);
            }
        });
    }
    private void setClick() {
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Toast.makeText(MainActivity.this,arrayList.get(position), Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, CityListActivity.class);
                intent.putExtra("provice", arrayList.get(position));
                startActivity(intent);
            }
        });
        exitTx.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    private void init() {
        exitTx = (TextView) findViewById(R.id.exit);
        listView = (ListView) findViewById(R.id.provice_list);
        proviceAdapter = new ProviceOrCityAdapter(MainActivity.this, arrayList);
        listView.setAdapter(proviceAdapter);
    }
}

代码不多,也不难,不多做解释。
里面使用了Adapter类,下一篇介绍。
博文链接:
android-基于Webservice实现天气信息获取及源码下载(一)
android-基于Webservice实现天气信息获取及源码下载(三)

源码下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值