Android基于中华万年历接口开发的WeatherReport天气预报

来源:互联网 发布:客户资料整理软件 编辑:程序博客网 时间:2024/05/01 17:49

效果展示

这里写图片描述

此界面仿QQ天气,只是界面元素没有那么多,后期考虑再加,得更换接口才行

天气API列表

  1. 中华万年历(可用),本项目中所使用的,当然也是在网上搜到的

    http://wthrcdn.etouch.cn/weather_mini?city=%E8%8B%8F%E5%B7%9E

  2. 中国天气网,到以下链接地址填写申请表,2016.12.13申请的,等待审核回复邮件,一直没回复,放弃

    http://smart.weather.com.cn/wzfw/smart/weatherapi.shtml

  3. 和风天气(可用),没有详细试过,郭神的第二行代码里的天气项目用的就是这个

  4. 彩云天气(可用),没有详细试过

  5. 新浪天气,以下是示例接口地址,直接点击可访问,查看网页源码可看到对应的xml结果

    http://php.weather.sina.com.cn/xml.php?city=%CB%D5%D6%DD&password=DJOYnieT8234jlsK&day=0

    day=0,表示当天,1表示明天,2表示后天,以此类推;
    如果在代码中引用,记得对cityName做一下编码处理

    URLEncoder.encode(cityName, “GB2312”)

    以下地址是天气图标示例

    http://php.weather.sina.com.cn/images/yb3/78_78/qing_0.png
    1),这里面的qing代表晴,换成对应天气的拼音即可;
    2),至于后面的数字:0代表白天,1代表夜间;
    3),78_78是指图标的大小,可以改成180_180。

    这里有一篇博文可以参考,关于新浪天气API开发的

    http://blog.csdn.net/KevinWu93/article/details/50086725

具体实战

1,获取接口返回数据

        Retrofit retrofit = new Retrofit.Builder()                .baseUrl("http://wthrcdn.etouch.cn")                .addConverterFactory(GsonConverterFactory.create())                .build();        RetrofitRequest service = retrofit.create(RetrofitRequest.class);        Call<WeatherBean> callback = service.getCityWeather(currCity);        callback.enqueue(new Callback<WeatherBean>() {            @Override            public void onResponse(Call<WeatherBean> call, Response<WeatherBean> response) {             WeatherBean weatherBean = response.body();             forecastList.addAll(weatherBean.getData().getForecast());             // 数据展示             mRecyclerView.setAdapter(new WeatherAdapter(WeatherActivity.this, forecastList));             showLineChart(lineChart, getLineData(), Color.TRANSPARENT);             ......             }            @Override            public void onFailure(Call<WeatherBean> call, Throwable t) {            }        });

2,retrofit接口

public interface RetrofitRequest {    @GET("/weather_mini?")    Call<WeatherBean>    getCityWeather(@Query("city") String name); }

3,折线图表展示

private void showLineChart(LineChart mChart, LineData mData, int color) {        mChart.setAutoScaleMinMaxEnabled(true);        Description description = new Description();        description.setText("");        mChart.setDescription(description); // 为空则图表右下角不显示描述        mChart.setDrawGridBackground(false);        mChart.setGridBackgroundColor(Color.TRANSPARENT);        mChart.setTouchEnabled(false);        mChart.setDragEnabled(false);        mChart.setScaleEnabled(false);        mChart.setPinchZoom(false);        mChart.setBackgroundColor(color);        mChart.setData(mData);        mChart.setExtraLeftOffset(10f); // y轴label距右边距距离,防止数据过大被遮盖        mChart.setExtraRightOffset(30f);        mChart.setExtraBottomOffset(10f);        mChart.invalidate();        XAxis xAxis = mChart.getXAxis();        xAxis.setEnabled(false);        YAxis yAxisRight = mChart.getAxisRight();        yAxisRight.setEnabled(false); // 不显示右边y轴标签        YAxis yAxis = mChart.getAxisLeft();        yAxis.setEnabled(false);        Legend mLegend = mChart.getLegend();        mLegend.setEnabled(false); // 不显示左下角的圆点和名称        mChart.animateX(1000);    }

4,图表数据

 private LineData getLineData() {        List<Entry> valsComp1 = new ArrayList<Entry>();        List<Entry> valsComp2 = new ArrayList<Entry>();        for (int i = 0; i < forecastList.size(); i++) {            Entry hc1e = new Entry(i, Float.valueOf(forecastList.get(i).getHigh().substring(2, forecastList.get(i).getHigh().length() - 1)));            valsComp1.add(hc1e);        }        for (int i = 0; i < forecastList.size(); i++) {            Entry lc1e = new Entry(i, Float.valueOf(forecastList.get(i).getLow().substring(2, forecastList.get(i).getLow().length() - 1)));            valsComp2.add(lc1e);        }        IValueFormatter formatter = new IValueFormatter() {            @Override            public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {                return (int) value + "°";            }        };        LineDataSet setComp1 = new LineDataSet(valsComp1, "高温");// 图例        setComp1.setAxisDependency(YAxis.AxisDependency.LEFT);        setComp1.setValueTextColor(ContextCompat.getColor(this, R.color.whiteseven));//曲线上文字的颜色        setComp1.setValueTextSize(13);// 文字值的大小        setComp1.setValueFormatter(formatter);// 文字值的样式,这里我们把原来的float类型显示为带温度单位的string类型        setComp1.setCircleRadius(3);// 白色圆点的半径        setComp1.setCircleColor(Color.WHITE);// 白色圆点的填充色        LineDataSet setComp2 = new LineDataSet(valsComp2, "低温");        setComp2.setAxisDependency(YAxis.AxisDependency.LEFT);        setComp2.setValueTextColor(ContextCompat.getColor(this, R.color.whiteseven));        setComp2.setValueTextSize(13);        setComp2.setValueFormatter(formatter);        setComp2.setCircleRadius(3);        setComp2.setCircleColor(Color.WHITE);        // use the interface ILineDataSet        List<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();        dataSets.add(setComp1);        dataSets.add(setComp2);        LineData data = new LineData(dataSets);        return data;    }

此功能已经集成到好旅App中,后面还将不断跟进优化

0 0
原创粉丝点击