wenserver获取天气预报数据 wenserver获取天气预报数据实例分享
人气:0package tao.cs;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class WeatherMain extends Activity {
Button btn01;
TextView tv01,tv02;
ImageView imgview01,imgview02;
EditText et01;
int int_img_1;
int int_img_2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//组件
// final EditText et01=(EditText) findViewById(R.id.et_TextWeather);
btn01=(Button) findViewById(R.id.Button01);
tv01=(TextView) findViewById(R.id.TextView01);
tv02=(TextView) findViewById(R.id.TextView02);
imgview01=(ImageView) findViewById(R.id.ImageView01);
imgview02=(ImageView) findViewById(R.id.ImageView02);
et01=(EditText) findViewById(R.id.EditText01);
btn01.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showWeather();
}
});
}//onCreate ***************end
protected void showWeather(){//显示所有的信息
String str_city=et01.getText().toString();
if(str_city.length()==0){
str_city="重庆";
}
// str_city="济南";
getWeather(str_city);
tv01.setText(getString_WeatherToday()); //今天天气
tv02.setText(getString_WeatherCurrent());//当前天气
imgview01.setImageResource(getIcon_1()); //当前的两个图标
imgview02.setImageResource(getIcon_2());
}//showWeather()*****************end
private static final String NAME_SPACE="http://WebXml.com.cn/";//命名空间 ,后期测试一下命名空间的作用
private static final String METHOD_NAME = "getWeatherbyCityName";
private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";// 投递SOAP数据的目标地址
private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";
protected void getWeather(String strcityname){//str 是城市名
/*
* 这里要做的工作是 提交客户端的请求,
* 然后获得服务端返回的复杂对象,后面会有专门的
*/
SoapObject sobject=new SoapObject(NAME_SPACE, METHOD_NAME);
sobject.addProperty("theCityName", strcityname);
AndroidHttpTransport ht=new AndroidHttpTransport(URL);
ht.debug=true;
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);//告诉SoapSerializationEnvelope把构造好的SoapObject封装进去:
envelope.bodyOut=sobject;
envelope.dotNet=true;
envelope.setOutputSoapObject(sobject);
try {
ht.call(SOAP_ACTION, envelope);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
//
SoapObject result=(SoapObject) envelope.bodyIn;
SoapObject detail=(SoapObject) result.getProperty("getWeatherbyCityNameResult");//类似于获取服务端返回复杂节点的一个内接点
parseWeather(detail);
}//getweather *****************end
String str_weather_today=null;
String str_weather_current=null;
protected void parseWeather(SoapObject so){
/*
* 解析复杂节点,并对相应的参数赋值,为后面的方法准备
*/
//第六个参数是概况,包括日期,天气....
String date=so.getProperty(6).toString();
str_weather_today="今天: "+date.split(" ")[0];
str_weather_today=str_weather_today+"\n气温:"+so.getProperty(5).toString();
str_weather_today=str_weather_today+"\n风力:"+so.getProperty(7).toString()+"\n";
//两个图标
int_img_1=parseIcon(so.getProperty(8).toString());
int_img_2=parseIcon(so.getProperty(9).toString());
str_weather_current=so.getProperty(10).toString();
}
public String getString_WeatherToday(){
return str_weather_today;
}
public String getString_WeatherCurrent(){
return str_weather_current;
}
public int getIcon_1(){
return int_img_1;
}
public int getIcon_2(){
return int_img_2;
}
public int parseIcon(String strIcon){
if (strIcon == null) return -1;
if ("0.gif".equals(strIcon)) return R.drawable.a_0;
if ("1.gif".equals(strIcon)) return R.drawable.a_1;
if ("2.gif".equals(strIcon)) return R.drawable.a_2;
if ("3.gif".equals(strIcon)) return R.drawable.a_3;
if ("4.gif".equals(strIcon)) return R.drawable.a_4;
if ("5.gif".equals(strIcon)) return R.drawable.a_5;
if ("6.gif".equals(strIcon)) return R.drawable.a_6;
if ("7.gif".equals(strIcon)) return R.drawable.a_7;
if ("8.gif".equals(strIcon)) return R.drawable.a_8;
if ("9.gif".equals(strIcon)) return R.drawable.a_9;
if ("10.gif".equals(strIcon)) return R.drawable.a_10;
if ("11.gif".equals(strIcon)) return R.drawable.a_11;
if ("12.gif".equals(strIcon)) return R.drawable.a_12;
if ("13.gif".equals(strIcon)) return R.drawable.a_13;
if ("14.gif".equals(strIcon)) return R.drawable.a_14;
if ("15.gif".equals(strIcon)) return R.drawable.a_15;
if ("16.gif".equals(strIcon)) return R.drawable.a_16;
if ("17.gif".equals(strIcon)) return R.drawable.a_17;
if ("18.gif".equals(strIcon)) return R.drawable.a_18;
if ("19.gif".equals(strIcon)) return R.drawable.a_19;
if ("20.gif".equals(strIcon)) return R.drawable.a_20;
if ("21.gif".equals(strIcon)) return R.drawable.a_21;
if ("22.gif".equals(strIcon)) return R.drawable.a_22;
if ("23.gif".equals(strIcon)) return R.drawable.a_23;
if ("24.gif".equals(strIcon)) return R.drawable.a_24;
if ("25.gif".equals(strIcon)) return R.drawable.a_25;
if ("26.gif".equals(strIcon)) return R.drawable.a_26;
if ("27.gif".equals(strIcon)) return R.drawable.a_27;
if ("28.gif".equals(strIcon)) return R.drawable.a_28;
if ("29.gif".equals(strIcon)) return R.drawable.a_29;
if ("30.gif".equals(strIcon)) return R.drawable.a_30;
if ("31.gif".equals(strIcon)) return R.drawable.a_31;
return 0;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
<Button
android:text="查询"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<ImageView
android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<ImageView
android:id="@+id/ImageView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/TextView01"
/>
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
加载全部内容