Weather report

        对于大多数的Android手机,天气app都是系统必备的,对于天气预报的功能的实现,最近也做了研究,其实也不是太复杂,主要用到了网络请求以及数据解析方面的内容。下面就来详细介绍下如何在android中实现天气appj基本功能的开发。
1.网络请求,在android系统中,网络请求主要有两种实现方式,URLConnection和httpClient,这两种方式都可以实现网络请求。
(1)URLConnection
        URL url = new URL(path);
        URLConnection conn = url.openConnection();
        conn.connect();
        InputStream in = conn.getInputStream();
        BufferedReader buffer = new BufferedReader(new InputStreamReader(in));
        String list = buffer.readLine();
        while (list != null) {
            xml += list;
            list = buffer.readLine();
        }
        这里的path就是我们要访问的网址,这样我们就可以将网页上的内容按照字符形式读取下来。存到list这个字符串中。(别忘了还要在注册文件中添加访问网络权限)
(2)HttpClient
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(path);
        HttpResponse response = client.execute(get);
        int code = response.getStatusLine().getStatusCode();
        if (code == 200) {
            InputStream reader = response.getEntity().getContent();
            BufferedReader buffer = new BufferedReader(new InputStreamReader(reader));
            String list = buffer.readLine();
            while (list != null) {
                xml += list;
                list = buffer.readLine();
            }
        }
        这里的path同样是网络地址,code是服务器给我们返回的一个值,如果是200,就说明访问成功,404就表示客户端异常,505就表示服务器端异常。不过用这种方式请求的话需要导入两个包httpclient和httpcore,这两个包都在studio文件夹的bin目录下,复制到你项目的libs目录下,还要进行导入,File-project structure-app-dependencies-'+'-File dependency,按照顺序然后选中你要导入的包,确认,就可以了。在导入这两个包后可能还要在build-grade目录下加入以下内容
packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
	exclude 'META-INF/NOTICE'
	exclude 'META-INF/LICENSE'
	exclude 'META-INF/DEPENDENCIES'
	exclude 'META-INF/notice.txt'
	exclude 'META-INF/license.txt'
	exclude 'META-INF/dependencies.txt'
	exclude 'META-INF/LGPL2.1'
}
        这样我们就可以访问网络上的资源了。对于免费的天气api,网上有很多,我用的是这个:http://www.k780.com/api/weather.future,很好用,提供的数据也很全面,实时天气,未来5-7天的,PM2.5和空气指数都有提供,我们只需要对需要的数据进行访问然后获取,接下来就是取出我们所需要的数据了,也就是JSON解析。
2.JSON解析
        JSON类型数据是很常见的数据类型,它比起XML来方便很多,它是以键值对的形式来存储数据的,所以很常用。解析起来非常容易,实例化一个JSONObject类,读取到网页上的内容,也就是上边的list传进去,即JSONObject json = new JSONObject(list),然后直接调用getString()把你需要的信息的键传进去,就能得到对应的值了,很容易。这就是我们实现天气预报的基本过程。下面是博主的一个小小的例子。
public class MainActivity extends Activity {

    private TextView temperature;
    private TextView weather;
    private TextView aqi;
    private TextView humidity;
    private TextView degree;
    private TextView location;
    private Button add;
    private Message msg;
    public  static Handler handler;
    public  static String position = "";
    public  static  boolean start = false;
    LocationClient locationClient = null;
    BDLocationListener myListener = new MyLocationListener();
    SwipeRefreshLayout swipeRefreshLayout;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        add = (Button) findViewById(R.id.buttonId);
        temperature = (TextView) findViewById(R.id.tempId);
        weather = (TextView) findViewById(R.id.weatherId);
        aqi = (TextView) findViewById(R.id.aqiId);
        humidity = (TextView) findViewById(R.id.humidityId);
        degree = (TextView) findViewById(R.id.degreeId);
        location = (TextView) findViewById(R.id.city);
        add.setOnClickListener(new ButtonListener());
        handler = new Myhandler();


        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefresh);		
        swipeRefreshLayout.setColorSchemeColors(android.R.color.background_light);
        swipeRefreshLayout.setSize(10);
        swipeRefreshLayout.setProgressBackgroundColor(android.R.color.white);
        swipeRefreshLayout.setOnRefreshListener(new reFreshListener());
        getLocationThread g = new getLocationThread();
                thread t = new thread();
                Thread T = new Thread(t, "Refresh");
                T.start();   
    }
    public static String getURLConnection(String path) {	
        String xml = "";
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet(path);
            HttpResponse response = client.execute(get);
            int code = response.getStatusLine().getStatusCode();
            if (code == 200) {
                InputStream reader = response.getEntity().getContent();
                BufferedReader buffer = new BufferedReader(new InputStreamReader(reader));
                String list = buffer.readLine();
                while (list != null) {
                    xml += list;
                    list = buffer.readLine();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return xml;
    }

    public class reFreshListener implements SwipeRefreshLayout.OnRefreshListener {<span>				

        @Override
        public void onRefresh() {
            thread t = new thread();
            Thread T = new Thread(t, "Refresh");
            T.start();<span style="white-space:pre">									
        }
    }


    private class thread implements Runnable {                                                      		

        @Override
        public void run() {
            String todayXML = "http://api.k780.com:88/?app=weather.today&weaid=";                   			
            String aqiXMl = "http://api.k780.com:88/?app=weather.pm25&weaid=";                    		        
            String futureXML = "http://api.k780.com:88/?app=weather.future&weaid=";                	         	
            String suffixXML = "&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";
            String todayWeather = getURLConnection(todayXML + position + suffixXML);
            String todayAqi = getURLConnection(aqiXMl + position + suffixXML);
            Message msg = new Message();
            Bundle bundle = new Bundle();
            bundle.putString("todayWeather", todayWeather);
            bundle.putString("todayAqi", todayAqi);
            msg.setData(bundle);
            try {
                Thread.sleep(1000);
                handler.sendMessage(msg);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    public class Myhandler extends Handler {
        public void handleMessage(Message msg) {
            swipeRefreshLayout.setRefreshing(false);
            String todayWeather = msg.getData().getString("todayWeather");
            if (todayWeather != null) {
                try {
                    todayWeather = String.valueOf(new JSONObject(todayWeather).getJSONObject("result"));
                    JSONObject json = new JSONObject(todayWeather);
                    location.setText(json.getString("citynm"));
                    String temp = json.getString("temp_curr");
                    final float scale = getApplicationContext().getResources().getDisplayMetrics().density;
                    if (temp.length() == 2) {
                        temperature.setWidth((int) (84 * scale + 0.5f));
                        System.out.println((int) (84 * scale + 0.5f));
                    } else if (temp.length() == 3) {
                        temperature.setWidth((int) (140 * scale + 0.5f));
                    }
                    temperature.setText(temp);
                    degree.setText("o");
                    weather.setText(json.getString("weather"));
                    humidity.setText("湿度" + json.getString("humidity") + " " + json.getString("wind") + json.getString("winp"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            String todayAqi = msg.getData().getString("todayAqi");
            if (todayAqi != null) {
                try {
                    todayAqi = String.valueOf(new JSONObject(todayAqi).getJSONObject("result"));
                    JSONObject json = new JSONObject(todayAqi);
                    aqi.setText("空气指数" + json.getString("aqi") + " " + json.getString("aqi_levnm") + "  " + json.getString("aqi_remark"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }
        上面就是实现获取实时天气的主要内容,其实很容易,但是要想实现更全面的功能就比较复杂了,本来我还想做一下定位功能,结果由于阅码能力太渣,读不懂大神们的代码,所以暂时没有实现这个功能,后续会跟进。不过关于定位大家可以用百度地图API, http://developer.baidu.com/map/index.php?title=android-locsdk上面有详细介绍,也可以下载它的Demo来仔细研读。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要通过接口实现语音播报,您需要使用一个支持 TTS 的语音 API。一些常用的语音 API 包括 Google Cloud Text-to-Speech、Amazon Polly 和 Microsoft Azure Cognitive Services。 以下是一个简单的示例脚本,它使用 jq 命令和 Google Cloud Text-to-Speech API 来将天气预报转换为语音并播放: ``` #!/bin/bash API_KEY="YOUR_API_KEY" CITY="New York" WEATHER_URL="http://api.openweathermap.org/data/2.5/weather?q=$CITY&appid=$API_KEY" LAST_WEATHER_FILE="last_weather.txt" LAST_WEATHER=$(cat $LAST_WEATHER_FILE 2>/dev/null) # Get current weather CURRENT_WEATHER=$(curl -s $WEATHER_URL | jq -r '.weather[0].description') # Check if there's a new weather report if [ "$LAST_WEATHER" != "$CURRENT_WEATHER" ]; then # Update last weather file echo "$CURRENT_WEATHER" > $LAST_WEATHER_FILE # Convert weather report to speech using Google Cloud Text-to-Speech curl -s -X POST -H "Content-Type: application/json" \ -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \ --data "{ 'input':{ 'text':'The current weather in $CITY is $CURRENT_WEATHER.' }, 'voice':{ 'languageCode':'en-US', 'name':'en-US-Wavenet-F' }, 'audioConfig':{ 'audioEncoding':'MP3' } }" "https://texttospeech.googleapis.com/v1/text:synthesize" | jq -r '.audioContent' | base64 --decode > weather.mp3 # Play weather report using a media player mpg123 weather.mp3 fi ``` 这个脚本会从 OpenWeatherMap API 获取当前天气状况,检查它是否与上一次运行时的状况不同,如果有新的状况,则将天气预报转换为语音并通过 Google Cloud Text-to-Speech API 播放它。您可以根据自己的需求修改脚本,并使用不同的语音 API 来实现语音播报。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值