Android Studio入门(6)——WebService

//使用webservice获取网络数据
//获取天气数据:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx
//ksoap2 :http://simpligility.github.io/ksoap2-android/getting-started.html
package com.example.admin.netcommunication;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Message;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//使用webservice获取网络数据
//获取天气数据:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx
//ksoap2 :http://simpligility.github.io/ksoap2-android/getting-started.html
public class WebServiceConnect extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
        WebService();
    }


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void WebService() {

        final  String SERVICE_NS = "http://WebXml.com.cn/";//命名空间
        final  String SOAP_ACTION = "http://WebXml.com.cn/getRegionCountry";//用来定义消息请求的地址,也就是消息发送到哪个操作
        final  String SERVICE_URL = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx";//URL地址,这里写发布的网站的本地地址
        String methodName = "getRegionCountry";
        //创建HttpTransportSE传输对象,该对象用于调用Web Service操作
        final HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
        ht.debug = true;
        final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);
        envelope.dotNet = true;
        envelope.bodyOut = soapObject;

        new Thread(){
            @Override
            public void run() {
                try {
                    try {
                        ht.call(SOAP_ACTION, envelope);
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (XmlPullParserException e) {
                        e.printStackTrace();
                    }
                    if(envelope.getResponse() != null){
                        List<String> list = new ArrayList<String>();
                        SoapObject so = (SoapObject) envelope.bodyIn;
                        SoapObject result = (SoapObject) so
                                .getProperty("getRegionCountry");
                        // 解析数据
                        for (int i = 0; i < result.getPropertyCount(); i++) {
                            list.add(result.getProperty(i).toString());
                        }

                        //result = so.getPropertyAsString(0);
                        //Message msg = new Message();
                        // msg.what = 1;
                        //handler.sendMessage(msg);
                    }
                    else{
                        // Message msg=new Message();
                        //msg.what=0;
                        //handler.sendMessage(msg);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}


 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值