package com.jiuqi.njt.ui.weather;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.jiuqi.njt.data.MyApp;
import com.jiuqi.njt.util.DateUtil;
import com.jiuqi.njt.util.RepairUtils;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
/**
* 获取天气数据异步类
*
* @author joe
*
*/
public class GetWeatherNewTaskNew extends AsyncTask<Void, Void, WeatherBean> {
private String tag = this.getClass().getName();
private String nameSpace = "com.jqyd.weather.service/weatherService";// 命名空间
private String methodName = "searchWeather"; // 调用的方法名称
private String methodNameOther = "getWeather"; // 调用的方法名称
private String endPoint = "http://116.255.134.166:9713/weatherService/services/weatherService"; // EndPoint
private String soapAction = "com.jqyd.weather.service/weatherService/searchWeather"; // SOAP
// Action
// private String endpoint =
// "http://116.255.134.166:9713/weatherService/services/weatherService?wsdl";
private String provinceName;
private String cityName;
private Context context;
private WeatherBean weather;
private boolean showDilag; // 是否显示获取天气对话框,天气服务应传值FALSE
private Dialog pd;
private SoapSerializationEnvelope envelope;
private String tID;
private boolean isaGetWeatherByTID; //是不是通过TID查询天气数据
private ReturnWeatherData returnWeatherData;
/**
*
* @param provinceName
* 省
* @param cityName
* 市
* @param context
* @param showDilag
* 是否需要显示获取数据对话框
*/
public GetWeatherNewTaskNew(String provinceName, String cityName,
Context context, boolean isShowDilag) {
super();
this.provinceName = provinceName;
this.cityName = cityName;
this.context = context;
this.showDilag = isShowDilag;
}
public interface ReturnWeatherData {
public void getWeatherData(WeatherBean weather);
}
/**
* 通过传入的省市数据将获取的WeatherBean通过returnWeatherData接口返回
* @param provinceName
* @param cityName
* @param context
* @param isShowDilag
* @param returnWeatherData 返回数据接口对象
*/
public GetWeatherNewTaskNew(String provinceName, String cityName,
Context context, boolean isShowDilag,ReturnWeatherData returnWeatherData) {
super();
this.provinceName = provinceName;
this.cityName = cityName;
this.context = context;
this.showDilag = isShowDilag;
this.returnWeatherData=returnWeatherData;
}
/**
*
* @param context
* @param showDilag
* 是否显示对话框
* @param tID
* 要查询的天气的tID
* @param returnWeatherData
* 返回数据接口对象
*/
public GetWeatherNewTaskNew(Context context, boolean showDilag, String tID,
ReturnWeatherData returnWeatherData) {
super();
this.context = context;
this.showDilag = showDilag;
this.tID = tID;
this.returnWeatherData = returnWeatherData;
isaGetWeatherByTID = true;
}
@Override
protected void onPreExecute() {
if (showDilag) {
pd = RepairUtils.myShowDialog(context, 2);
}
if(null==provinceName||null==cityName){
provinceName =( (MyApp)context.getApplicationContext()).getProvince();
cityName =( (MyApp)context.getApplicationContext()).getCity();
}
SoapObject rpc = null;
if(isaGetWeatherByTID){
// 指定WebService的命名空间和调用的方法名
rpc = new SoapObject(nameSpace, methodNameOther);
// 设置需调用WebService接口需要传入的一个参数:TID
rpc.addProperty("in0", tID);
}else{
// 指定WebService的命名空间和调用的方法名
rpc = new SoapObject(nameSpace, methodName);
// 设置需调用WebService接口需要传入的三个参数:省、市、时间(格式:yyyyMMdd)
rpc.addProperty("in0", provinceName.replace("省", ""));
rpc.addProperty("in1", cityName.replace("市", ""));
rpc.addProperty("in2",DateUtil.getTimeStamp(new Date(), DateUtil.YYYYMMDD));
}
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.bodyOut = rpc;
// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = true;
// 等价于envelope.bodyOut = rpc;
envelope.setOutputSoapObject(rpc);
weather = null;
super.onPreExecute();
}
@Override
protected WeatherBean doInBackground(Void... arg0) {
// TODO Auto-generated method stub
try {
String result = "";
HttpTransportSE transport = new HttpTransportSE(endPoint);
try {
// 调用WebService
transport.call(soapAction, envelope);
} catch (Exception e) {
e.printStackTrace();
}
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
// 获取返回的结果
result = object.getProperty(0).toString();
// 将从WebService返回的结果转成流,通过XML解析获取到结果里的数据
InputStream input = new ByteArrayInputStream(result.getBytes());
weather = doParse(input);
// 将WebService返回的结果返回
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return weather;
}
public WeatherBean getRemoteInfo(String provinceName, String cityName) {
return weather;
}
@Override
protected void onPostExecute(WeatherBean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(null!=returnWeatherData){
returnWeatherData.getWeatherData(result);
}
if (showDilag) {
RepairUtils.myRemoveDialog(pd);
}
}
/**
* 将获取到的天气数据进行XML解析
*
* @param in
* 结果的流
* @return
*/
public WeatherBean doParse(InputStream in) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(in);
NodeList list = doc.getElementsByTagName("info");
for (int i = 0; i < list.getLength(); i++) {
// 提取WeatherBean元素
Element el = (Element) list.item(i);
weather = new WeatherBean();
weather.setWheatherId(getSubelementTextContentByName(el,
"wheatherId"));
weather.setAirQuality(getSubelementTextContentByName(el,
"airQuality"));
String todayWheather = getSubelementTextContentByName(el,
"todayWeather");
Log.e(tag, todayWheather);
if (null != todayWheather) {
String[] weatherData = todayWheather.split(";");
if (null != weatherData && weatherData.length > 1) {
weather.setOneWind(weatherData[1].split(":")[1]);
}
}
// 获取ID节点
String oneWeather = getSubelementTextContentByName(el, "one");
Log.e(tag, oneWeather);
String[] data = oneWeather.replaceAll(" ", "").split("\n");
data[1] = data[1].split("日")[1];
weather.setOneCloud(data[1]);
weather.setOneCelsius(data[2]);
weather.setOneImage1(data[1]);
// weather.setOneWind(data[3]);
// 获取ID节点
String twoWeather = getSubelementTextContentByName(el, "two");
data = twoWeather.replaceAll(" ", "").split("\n");
data[1] = data[1].split("日")[1];
weather.setTwoCloud(data[1]);
weather.setTwoCelsius(data[2]);
weather.setTwoImage1(data[1]);
weather.setTwoWind(data[3]);
// 获取ID节点
String threeWeather = getSubelementTextContentByName(el,
"three");
data = threeWeather.replaceAll(" ", "").split("\n");
data[1] = data[1].split("日")[1];
weather.setThreeCloud(data[1]);
weather.setThreeCelsius(data[2]);
weather.setThreeImage1(data[1]);
weather.setThreeWind(data[3]);
// 获取ID节点
String fourWeather = getSubelementTextContentByName(el, "four");
data = fourWeather.replaceAll(" ", "").split("\n");
data[1] = data[1].split("日")[1];
weather.setFourCloud(data[1]);
weather.setFourCelsius(data[2]);
weather.setFourImage1(data[1]);
weather.setFourWind(data[3]);
// 获取ID节点
String fiveWeather = getSubelementTextContentByName(el, "five");
data = fiveWeather.replaceAll(" ", "").split("\n");
data[1] = data[1].split("日")[1];
weather.setFiveCloud(data[1]);
weather.setFiveCelsius(data[2]);
weather.setFiveImage1(data[1]);
weather.setFiveWind(data[3]);
}
return weather;
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 得到指定元素的子元素文本 节点(
*
* @param el
* 父元素
* @param name
* 子元素名称
* @return
*/
@SuppressLint("NewApi")
private String getSubelementTextContentByName(Element el, String name) {
NodeList list = el.getElementsByTagName(name);
Element e = (Element) list.item(0);
return e.getTextContent();
}
}