安卓小项目之天气预报
来源:互联网 发布: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
- 安卓小项目之天气预报
- Android项目 之 天气预报(附源码)
- Android项目之天气预报 的实现分析
- Android项目->天气预报项目
- Android[项目] Android天气预报
- 分享android天气预报项目
- 项目总结-天气预报(电视)
- 天气预报项目学习总结
- 天气预报项目学习总结
- WebService 之调用天气预报
- webservice之获取天气预报
- Json解析之天气预报
- WeatherForecast之实现天气预报
- WebService之获取天气预报
- 微信小程序之天气预报
- Android完整项目_天气预报
- 一个项目搞定Android开发, Google天气预报开发之API的分析
- 【源码分享下载】每日更新之Android项目源码---手机天气预报系统
- 蓝牙核心技术概述(三): 蓝牙协议规范(射频、基带链路控制、链路管理)
- SQL聚集函数
- 1072. Gas Station
- HDOJ 4324 Triangle LOVE (三角恋,待解决)
- java synchronized详解
- 安卓小项目之天气预报
- Android开发中的问题总结
- 神奇的“空格”
- C学习笔记
- Java JDBC 进行批量跟新操作
- 【图像识别】【读论文】
- ActivityLifecycleCallbacks管理某些Aty的周期
- 批处理(bat)常用指令及其作用
- 先序中序得到后序 (MEDIUM)