Android之简易天气预报小应用(xml解析练手)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ygd.jreduch09.WeatherTestActivity">
    <Spinner
        android:id="@+id/sp1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </Spinner>
    <Spinner
        android:id="@+id/sp2"
        android:layout_below="@+id/sp1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </Spinner>

    <TextView
        android:id="@+id/tv"
        android:layout_below="@id/sp2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="12℃/20℃"
        android:layout_centerHorizontal="true"
        android:textSize="50sp"
        android:layout_marginTop="30dp"/>
    <TextView
        android:id="@+id/tv0"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="60dp"
        android:layout_below="@id/tv"
        android:text="今日天气实况:气温:14℃;风向/风力:东南风 1级;湿度:65%;紫外线强度:暂缺。空气质量:暂缺。"
       android:textSize="24sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <GridLayout
        android:id="@+id/gl"
        android:layout_below="@id/tv0"
        android:orientation="horizontal"
        android:columnCount="4"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/tv1"
            android:text="5月10日 晴"
            android:layout_columnWeight="1"
            android:textSize="18sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView
            android:id="@+id/iv1"
            android:src="@mipmap/a"
            android:layout_columnWeight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <ImageView
            android:id="@+id/iv2"
            android:src="@mipmap/b"
            android:layout_columnWeight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/tv2"
            android:layout_columnWeight="1"
            android:textSize="18sp"
            android:text="西北风转西南风3-4级"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tv3"
            android:text="5月10日 晴"
            android:layout_columnWeight="1"
            android:textSize="18sp"
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView
            android:id="@+id/iv3"
            android:src="@mipmap/a"
            android:layout_marginTop="10dp"
            android:layout_columnWeight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <ImageView
            android:id="@+id/iv4"
            android:src="@mipmap/b"
            android:layout_columnWeight="1"
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/tv4"
            android:layout_columnWeight="1"
            android:textSize="18sp"
            android:text="16℃/27℃"
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tv5"
            android:text="5月10日 晴"
            android:layout_columnWeight="1"
            android:textSize="18sp"
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView
            android:id="@+id/iv5"
            android:src="@mipmap/a"
            android:layout_marginTop="10dp"
            android:layout_columnWeight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <ImageView
            android:id="@+id/iv6"
            android:src="@mipmap/b"
            android:layout_columnWeight="1"
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/tv6"
            android:layout_columnWeight="1"
            android:textSize="18sp"
            android:layout_marginTop="10dp"
            android:text="11℃/25℃"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


    </GridLayout>
    <!--<LinearLayout-->
        <!--android:id="@+id/ll"-->
        <!--android:layout_below="@id/sp2"-->
        <!--android:orientation="horizontal"-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="wrap_content">-->
        <!--<ImageView-->
            <!--android:id="@+id/iv1"-->
            <!--android:layout_width="wrap_content"-->
            <!--android:layout_height="wrap_content" />-->
        <!--<ImageView-->
            <!--android:id="@+id/iv2"-->
            <!--android:layout_width="wrap_content"-->
            <!--android:layout_height="wrap_content" />-->
        <!--<ImageView-->
            <!--android:id="@+id/iv3"-->
            <!--android:layout_width="wrap_content"-->
            <!--android:layout_height="wrap_content" />-->
        <!--<ImageView-->
            <!--android:id="@+id/iv4"-->
            <!--android:layout_width="wrap_content"-->
            <!--android:layout_height="wrap_content" />-->
        <!--<ImageView-->
            <!--android:id="@+id/iv5"-->
            <!--android:layout_width="wrap_content"-->
            <!--android:layout_height="wrap_content" />-->
        <!--<ImageView-->
            <!--android:id="@+id/iv6"-->
            <!--android:layout_width="wrap_content"-->
            <!--android:layout_height="wrap_content" />-->

    <!--</LinearLayout>-->
    <!--<TextView-->
        <!--android:id="@+id/tv"-->
        <!--android:layout_below="@+id/ll"-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="match_parent" />-->

</RelativeLayout>
  • 主函数代码:WeatherTestActivity.java
public class WeatherTestActivity extends AppCompatActivity {
    private String URL_PROVINCE="http://www.webxml.com.cn/webservices/weatherwebservice.asmx/getSupportProvince";
    private String URL_CITY="http://www.webxml.com.cn/webservices/weatherwebservice.asmx/getSupportCity";
    private String URL_WEATHER="http://www.webxml.com.cn/webservices/weatherwebservice.asmx/getWeatherbyCityName";

    private List provinceList,cityList;

    private Spinner sp1,sp2;
    private TextView tv,tv0,tv1,tv2,tv3,tv4,tv5,tv6;
    private ImageView[] iv=new ImageView[6];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather_test);
        sp1= (Spinner) findViewById(R.id.sp1);
        sp2= (Spinner) findViewById(R.id.sp2);
        tv= (TextView) findViewById(R.id.tv);
        tv0= (TextView) findViewById(R.id.tv0);
        tv1= (TextView) findViewById(R.id.tv1);
        tv2= (TextView) findViewById(R.id.tv2);
        tv3= (TextView) findViewById(R.id.tv3);
        tv4= (TextView) findViewById(R.id.tv4);
        tv5= (TextView) findViewById(R.id.tv5);
        tv6= (TextView) findViewById(R.id.tv6);
        iv[0]= (ImageView) findViewById(R.id.iv1);
        iv[1]= (ImageView) findViewById(R.id.iv2);
        iv[2]= (ImageView) findViewById(R.id.iv3);
        iv[3]= (ImageView) findViewById(R.id.iv4);
        iv[4]= (ImageView) findViewById(R.id.iv5);
        iv[5]= (ImageView) findViewById(R.id.iv6);
        new MyDoGetProvince().execute(URL_PROVINCE);
        sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Log.d("===","pos="+position+",id="+id);
                if(provinceList!=null&&provinceList.size()>0){
                    String url=URL_CITY+"?byProvinceName="+provinceList.get(position);
                    new MyDoGetCity().execute(url);
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(cityList!=null&&cityList.size()>0){
                    String city=cityList.get(position).toString().substring(0,2);
                    Log.d("===city",""+city);
                    String url=URL_WEATHER+"?theCityName="+city;
                    new MyDoGetWeather().execute(url);
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }


    public class MyDoGetProvince extends AsyncTask<String,Void,List>{

        @Override
        protected List doInBackground(String... params) {
            return HttpUtil.doGetXml(params[0]);
        }

        @Override
        protected void onPostExecute(List list) {
            super.onPostExecute(list);
            if(list!=null&&list.size()>0){
                provinceList=list;
                ArrayAdapter aa=new ArrayAdapter(getBaseContext(),android.R.layout.simple_spinner_item,provinceList);
                sp1.setAdapter(aa);
            }

        }
    }


    public class MyDoGetCity extends AsyncTask<String,Void,List>{

        @Override
        protected List doInBackground(String... params) {
            return HttpUtil.doGetXml(params[0]);
        }

        @Override
        protected void onPostExecute(List list) {
            super.onPostExecute(list);
            if(list!=null&&list.size()>0){
                cityList=list;
                ArrayAdapter aa=new ArrayAdapter(getBaseContext(),android.R.layout.simple_spinner_item,cityList);
                sp2.setAdapter(aa);
            }

        }
    }


    public class MyDoGetWeather extends AsyncTask<String,Void,List>{

        @Override
        protected List doInBackground(String... params) {
            return HttpUtil.doGetXml(params[0]);
        }

        @Override
        protected void onPostExecute(List list) {
            int[] img=new int[32];
            img[0]=R.mipmap.a;
            img[1]=R.mipmap.b;
            img[2]=R.mipmap.c;
            img[3]=R.mipmap.d;
            img[4]=R.mipmap.e;
            img[5]=R.mipmap.f;
            img[6]=R.mipmap.g;
            img[7]=R.mipmap.h;
            img[8]=R.mipmap.i;
            img[9]=R.mipmap.j;
            img[10]=R.mipmap.k;
            img[11]=R.mipmap.l;
            img[12]=R.mipmap.m;
            img[13]=R.mipmap.n;
            img[14]=R.mipmap.o;
            img[15]=R.mipmap.p;
            img[16]=R.mipmap.q;
            img[17]=R.mipmap.r;
            img[18]=R.mipmap.s;
            img[19]=R.mipmap.t;
            img[20]=R.mipmap.u;
            img[21]=R.mipmap.v;
            img[22]=R.mipmap.w;
            img[23]=R.mipmap.x;
            img[24]=R.mipmap.y;
            img[25]=R.mipmap.z;
            img[26]=R.mipmap.aa;
            img[27]=R.mipmap.bb;
            img[28]=R.mipmap.cc;
            img[29]=R.mipmap.dd;
            img[30]=R.mipmap.ee;
            img[31]=R.mipmap.ff;
            super.onPostExecute(list);
            if(list!=null&&list.size()>0){
                StringBuilder sbd=new StringBuilder();
                Log.d("===listsize",""+list.size());
                int n=0;
                for(int i=0;i<list.size();i++){
                    String wea=list.get(i).toString();
                    if(wea.contains("gif")){
                        int index=wea.indexOf(".");
                        Log.d("===index",""+index);
                        int k=Integer.parseInt(wea.substring(0,index));
                        iv[n++].setImageResource(img[k]);
                    }else if(i==10){
                        tv.setText(wea);
                    }else if(i==12){
                        tv1.setText(wea);
                    }else if(i==14){
                        tv2.setText(wea);
                    }else if(i==20){
                        tv0.setText(wea);
                    }else if(i==26){
                        tv3.setText(wea);
                    }else if(i==24){
                        tv4.setText(wea);
                    }else if(i==36){
                        tv5.setText(wea);
                    }else if(i==34){
                        tv6.setText(wea);
                    }
                }
            }

        }
    }

}
  • 解析xml用到的代码:PullXmlUtil.java
public class PullXmlUtil {
    private InputStream is;
    public PullXmlUtil(InputStream is){
        this.is=is;
    }
    public List getAllElement() throws XmlPullParserException, IOException {
        List myData=null;
        XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
        XmlPullParser xpp=factory.newPullParser();
        xpp.setInput(is,"UTF-8");
        int eventType=xpp.getEventType();
        String elementName="";
        while(eventType!=XmlPullParser.END_DOCUMENT){
            switch (eventType){
                case XmlPullParser.START_DOCUMENT:
                    myData=new ArrayList();
                    break;
                case XmlPullParser.START_TAG:
                    elementName=xpp.getName();
                    break;
                case XmlPullParser.TEXT:
                    if(elementName.equals("string"))
                        myData.add(xpp.getText());
                    break;
                case XmlPullParser.END_TAG:
                    break;
            }
            eventType=xpp.next();
        }
        return myData;
    }
}
  • 连接网络用到的代码:HttpUtile.java
public class HttpUtil {
 public static List doGetXml(String u){
        HttpURLConnection con=null;
        InputStream is=null;
        List list=null;
        try {
            URL url=new URL(u);
            con= (HttpURLConnection) url.openConnection();
            con.setConnectTimeout(5*1000);  //设置超时时间
            con.setReadTimeout(5*1000);     //设置读取时间
            if(con.getResponseCode()==200){ //判断是否连接成功
                is=con.getInputStream();    //获取输入
                PullXmlUtil pullXmlUtil=new PullXmlUtil(is);
                try {
                    list=pullXmlUtil.getAllElement();
                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                }

            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(is!=null){
                    is.close();
                }
                if(con!=null){
                    con.disconnect();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }
}
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值