安卓小项目之天气预报

来源:互联网 发布:java求质数 编辑:程序博客网 时间:2024/06/05 02:52

项目名:刀刀天气预报

api源:百度天气预报api

设计界面:略粗糙



mainactivity

package com.example.zheng.daodaomap;import android.os.AsyncTask;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.method.ScrollingMovementMethod;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;public class MainActivity extends AppCompatActivity {    private TextView result;    private EditText cityname;    String rdline=new String();    String read=null;    int i=0;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);       cityname=(EditText) findViewById(R.id.cityname);        result=(TextView)findViewById(R.id.result);        Button btsearch=(Button)findViewById(R.id.btsearch);        btsearch.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                new AsyncTask<String,Void,Void>(){                    @Override                    protected Void doInBackground(String... params) {                        try {                            URL url=new URL(params[0]);                            try {                                URLConnection conn=url.openConnection();                                InputStream is=conn.getInputStream();                                InputStreamReader isr=new InputStreamReader(is,"utf-8");                                BufferedReader br=new BufferedReader(isr);                                String line;                                while((line=br.readLine())!=null)                                {                                    System.out.println(line);                                    rdline+=line;                                }                                br.close();                                isr.close();                                is.close();                            } catch (IOException e) {                                e.printStackTrace();                            }                        } catch (MalformedURLException e) {                            e.printStackTrace();                        }                        return null;                    }                }.execute("http://api.map.baidu.com/telematics/v3/weather?location="+cityname.getText()+"&output=xml&ak=GuZriL3rkm1MUnyTyfsNGvTC");                gettqub(rdline);                result.setText(read);                rdline="";                read="";            }        });    }    public void gettqub(String rdline)    {        try {            Document document= DocumentHelper.parseText(rdline);            Element root=document.getRootElement();            Element results=root.element("results");            Element currentCity=results.element("currentCity");//获得当前城市名            Element weather_data=results.element("weather_data");            Element date1=weather_data.element("date");//获得当前时间            Element weather1=weather_data.element("weather");//获得当前天气            Element wind1=weather_data.element("wind");//获取风            Element temperature1=weather_data.element("temperature");//获取温度            //建议===================================================================            Element index=results.element("index");            Element title1=index.element("title");            Element zs=index.element("zs");            Element tipt=index.element("tipt");            Element des=index.element("des");            read="查询结果:"+"\r\n"+"当前城市:"+currentCity.getText()+"\r\n"+"当前时间:"+date1.getText()+"\r\n"+"风速:"+wind1.getText()+"\r\n"+"气温:"+temperature1.getText()+"\r\n"+"给您的建议:"+"\r\n"+title1.getText()+"\r\n"+zs.getText()+"\r\n"+tipt.getText()+"\r\n"+des.getText();        } catch (DocumentException e) {            e.printStackTrace();        }    }}

activity_main.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:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.zheng.daodaomap.MainActivity">    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:inputType="textPersonName"        android:text="请输入城市名称"        android:ems="10"        android:id="@+id/cityname"        android:layout_alignParentTop="true"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true" />    <Button        android:text="查询天气预报"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/cityname"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true"        android:id="@+id/btsearch" />    <TextView        android:text="查询结果:"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/btsearch"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true"        android:layout_marginTop="12dp"        android:id="@+id/result" />    <TextView        android:text="zsd出品"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:layout_marginBottom="28dp"        android:id="@+id/textView2" /></RelativeLayout>

输入天津测试下:



最后温馨提示下,需要添加相应的权限哦。

1 0
原创粉丝点击