//使用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();
}
}