wenserver獲取天氣預(yù)報(bào)數(shù)據(jù)實(shí)例分享
package 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="濟(jì)南";
getWeather(str_city);
tv01.setText(getString_WeatherToday()); //今天天氣
tv02.setText(getString_WeatherCurrent());//當(dāng)前天氣
imgview01.setImageResource(getIcon_1()); //當(dāng)前的兩個(gè)圖標(biāo)
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數(shù)據(jù)的目標(biāo)地址
private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";
protected void getWeather(String strcityname){//str 是城市名
/*
* 這里要做的工作是 提交客戶端的請求,
* 然后獲得服務(wù)端返回的復(fù)雜對象,后面會有專門的
*/
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把構(gòu)造好的SoapObject封裝進(jìn)去:
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");//類似于獲取服務(wù)端返回復(fù)雜節(jié)點(diǎn)的一個(gè)內(nèi)接點(diǎn)
parseWeather(detail);
}//getweather *****************end
String str_weather_today=null;
String str_weather_current=null;
protected void parseWeather(SoapObject so){
/*
* 解析復(fù)雜節(jié)點(diǎn),并對相應(yīng)的參數(shù)賦值,為后面的方法準(zhǔn)備
*/
//第六個(gè)參數(shù)是概況,包括日期,天氣....
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風(fēng)力:"+so.getProperty(7).toString()+"\n";
//兩個(gè)圖標(biāo)
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>
- Vista天氣預(yù)報(bào)--修正不顯示中國天氣預(yù)報(bào)的BUG
- Au3截取騰訊天氣的腳本
- js日歷控件(可精確到分鐘)
- php+javascript的日歷控件
- JS學(xué)習(xí)之一個(gè)簡易的日歷控件
- javascript實(shí)現(xiàn)日歷控件(年月日關(guān)閉按鈕)
- 修改js Calendar日歷控件 兼容IE9/谷歌/火狐
- 簡約JS日歷控件 實(shí)例代碼
- .net mvc頁面UI之Jquery博客日歷控件實(shí)現(xiàn)代碼
- 利用中國天氣預(yù)報(bào)接口實(shí)現(xiàn)簡單天氣預(yù)報(bào)
- jquery日歷控件實(shí)現(xiàn)方法分享
- php使用百度天氣接口示例
- 微信公眾平臺天氣預(yù)報(bào)功能開發(fā)
- 日歷控件和天氣使用分享
相關(guān)文章
flutter監(jiān)聽app進(jìn)入前后臺狀態(tài)的實(shí)現(xiàn)
在開發(fā)app的過程中,我們經(jīng)常需要知道app處于前后臺的狀態(tài),本文主要介紹了flutter監(jiān)聽app進(jìn)入前后臺狀態(tài)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04android自定義控件和自定義回調(diào)函數(shù)步驟示例
這篇文章主要介紹了android自定義控件步驟示例,包括為View類增加屬性、響應(yīng)用戶消息、自定義回調(diào)函數(shù)等方法2014-01-01Android13?加強(qiáng)Intent?filters?的安全性
這篇文章主要介紹了Android13?加強(qiáng)Intent?filters?的安全性,文章基于Android13?展開Intent?filters?安全性加強(qiáng)的詳細(xì)介紹,需要的小伙伴可以參考一下2022-05-05Android 獲取服務(wù)器與客戶端時(shí)差的實(shí)例代碼
下面小編就為大家分享一篇Android 獲取服務(wù)器與客戶端時(shí)差的實(shí)例代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01