和风天气api说明地址: http://www.heweather.com/documents/api/s6
项目需要用到天气的数据来做测试,所以学习了一下和风天气这个平台的使用方法,个人觉得还是很全面的。
使用之前需要自己在和风天气平台上面注册一个用户,然后点击控制器复制一下自己的key,后面会用到。
代码:
我获取的就是今天明天和后天三天的天气加上pm2.5,看过开发文档就知道pm2.5是在另外一个api上面,所以我就需要两个api接口
// 接口地址
private String API = "https://free-api.heweather.com/s6/weather?key=加上你自己的key&location=";
// 获取Pm25的接口地址
private String getPm25URL = "https://free-api.heweather.com/s6/air/now?key=加上你自己的key&location=";
接口的形式也很简单,就是
https://free-api.heweather.com/s6/weather?key=key&location=
需要注意的是:https://free-api.heweather.com/s6/weather?key= 这一段,不是所有人都是一样的,,就是你自己需要哪种数据就去找对应的api,因为我自己就是去看别人的代码然后提示,某个属性不存在,,233,然后key也是每个人不一样的,key和location都是必须的值。location是中文的话需要处理乱码。只需要处理location。
Java代码:
因为这个是为了测试不同地方的数据是否一致然后又不能开车到那个地方去,所以需要输入地名,然后可以输入的地名平台上面也有,http://www.heweather.com/documents/city
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binID();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String city = editText.getText().toString();
String c = "";
try {
// 解决输入的地址是中文的情况
c = URLEncoder.encode(city, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
new MyWeather().execute(API + c);
new getPm25().execute(getPm25URL + c);
}
});
}
class MyWeather extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... strings) {
StringBuffer stringBuffer = null;// 相当于一个 字符串,只不过线程更安全
try {
URL url = new URL(strings[0]);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
InputStream inputStream = null;// 获取要输入的数据
if (httpURLConnection.getResponseCode() == 200) {
// 接收结果
inputStream = httpURLConnection.getInputStream();
// 检测网络异常
} else {
return "11";
}
InputStreamReader reader = new InputStreamReader(inputStream,
"UTF-8");// 读取输入的数据,解决乱码
BufferedReader bufferedReader = new BufferedReader(reader);// 缓存流
stringBuffer = new StringBuffer();
String timp = null;
// 缓冲逐行读取
while ((timp = bufferedReader.readLine()) != null) {
stringBuffer.append(timp);
}
// 关闭流
inputStream.close();
reader.close();
bufferedReader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuffer.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (s.equals("11")) {
Toast.makeText(MainActivity.this, "网络异常", Toast.LENGTH_SHORT)
.show();
}
try {
JSONObject object = new JSONObject(s);
JSONObject object1 = object.getJSONArray("HeWeather6")
.getJSONObject(0);
// 获取未来三天的温度天气日期
JSONObject obj0 = null;
JSONObject obj1 = null;
JSONObject obj2 = null;
// 预报的三天
JSONArray daily_forecast = object1
.getJSONArray("daily_forecast");
obj0 = daily_forecast.getJSONObject(0);// 今天的
tv_today_date.setText("今天日期:" + obj0.getString("date"));
tv_today_temperature.setText("今天温度:"
+ obj0.getString("tmp_min") + "~"
+ obj0.getString("tmp_max"));
tv_today_weatherName.setText("今天天气:"
+ obj0.getString("cond_txt_d") + "~"
+ obj0.getString("cond_txt_n"));// 这个天气状况可能会有两种,白天跟晚上不同,是都要显示出来吗
obj1 = daily_forecast.getJSONObject(1);// 明天的
tv_tomorrow_date.setText("明天日期:" + obj1.getString("date"));
tv_tomorrow_temperature.setText("明天温度:"
+ obj1.getString("tmp_min") + "~"
+ obj1.getString("tmp_max"));
tv_tomorrow_weatherName.setText("明天天气:"
+ obj1.getString("cond_txt_d") + "~"
+ obj1.getString("cond_txt_n"));
obj2 = daily_forecast.getJSONObject(2);// 后天的
tv_dayaftertomorrow_date.setText("后天日期:"
+ obj2.getString("date"));
tv_dayaftertomorrow_temperature.setText("后天温度:"
+ obj2.getString("tmp_min") + "~"
+ obj2.getString("tmp_max"));
tv_dayaftertomorrow_weatherName.setText("后天天气:"
+ obj2.getString("cond_txt_d") + "~"
+ obj2.getString("cond_txt_n"));
// 实时的天气
JSONObject now = object1.getJSONObject("now");
tv_now_temperature.setText("实时温度:" + now.getString("tmp"));// 两个温度貌似是一样的
tv_now_weatherName
.setText("实时 天气:" + now.getString("cond_txt"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void binID() {
editText = (EditText) findViewById(R.id.main_editView);
button = (Button) findViewById(R.id.main_button);
tv_now_temperature = (TextView) findViewById(R.id.tv_now_temperature);
tv_now_weatherName = (TextView) findViewById(R.id.tv_now_weatherName);
tv_now_pm2_5 = (TextView) findViewById(R.id.tv_now_pm2_5);
tv_today_date = (TextView) findViewById(R.id.tv_today_date);
tv_today_temperature = (TextView) findViewById(R.id.tv_today_temperature);
tv_today_weatherName = (TextView) findViewById(R.id.tv_today_weatherName);
// tv_today_PM2_5 = (TextView) findViewById(R.id.tv_today_PM2_5);
tv_tomorrow_date = (TextView) findViewById(R.id.tv_tomorrow_date);
tv_tomorrow_temperature = (TextView) findViewById(R.id.tv_tomorrow_temperature);
tv_tomorrow_weatherName = (TextView) findViewById(R.id.tv_tomorrow_weatherName);
// tv_tomorrow_PM2_5 = (TextView) findViewById(R.id.tv_tomorrow_PM2_5);
tv_dayaftertomorrow_date = (TextView) findViewById(R.id.tv_dayaftertomorrow_date);
tv_dayaftertomorrow_temperature = (TextView) findViewById(R.id.tv_dayaftertomorrow_temperature);
tv_dayaftertomorrow_weatherName = (TextView) findViewById(R.id.tv_dayaftertomorrow_weatherName);
// tv_dayaftertomorrow_PM2_5 = (TextView)
// findViewById(R.id.tv_dayaftertomorrow_PM2_5);
}
class getPm25 extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
StringBuffer stringBuffer = null;
try {
URL url = new URL(params[0]);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
InputStream inputStream = null;
if (httpURLConnection.getResponseCode() == 200) {
// 接收结果
inputStream = httpURLConnection.getInputStream();
// 检测网络异常
} else {
return "11";
}
InputStreamReader reader = new InputStreamReader(inputStream,
"UTF-8");
BufferedReader bufferedReader = new BufferedReader(reader);
stringBuffer = new StringBuffer();
String timp = null;
// 缓冲逐行读取
while ((timp = bufferedReader.readLine()) != null) {
stringBuffer.append(timp);
}
// 关闭流
inputStream.close();
reader.close();
bufferedReader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuffer.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (s.equals("11")) {
Toast.makeText(MainActivity.this, "网络异常", Toast.LENGTH_SHORT)
.show();
}
JSONObject object;
try {
object = new JSONObject(s);
JSONObject object1 = object.getJSONArray("HeWeather6")
.getJSONObject(0);
JSONObject pm25Obj = object1.getJSONObject("air_now_city");
String pm25 = pm25Obj.getString("pm25");
tv_now_pm2_5.setText("实时pm2.5:" + pm25);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<!-- 实时的天气。整个长:1280,整个宽:480,上面菜单栏:55 -->
<EditText
android:id="@+id/main_editView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="请输入城市名称" />
<Button
android:id="@+id/main_button"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="查询" />
<TextView
android:id="@+id/tv_now_temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:textColor="@color/white"
android:textSize="@dimen/normalSize" />
<TextView
android:id="@+id/tv_now_weatherName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:textColor="@color/white"
android:textSize="@dimen/normalSize" />
<TextView
android:id="@+id/tv_now_pm2_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:textColor="@color/white"
android:textSize="@dimen/normalSize" />
<!-- 今明后天的天气 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<!-- 今天的天气 -->
<LinearLayout
android:id="@+id/today_weather_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- 最上面的日期 -->
<TextView
android:id="@+id/tv_today_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="@dimen/normalSize" />
<TextView
android:id="@+id/tv_today_temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:textColor="@color/white"
android:textSize="@dimen/normalSize" />
<TextView
android:id="@+id/tv_today_weatherName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:textColor="@color/white"
android:textSize="@dimen/normalSize" />
<TextView
android:id="@+id/tv_today_PM2_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:textColor="@color/white"
android:textSize="@dimen/normalSize"
android:visibility="gone" />
</LinearLayout>
<!-- 明天的天气 -->
<LinearLayout
android:id="@+id/tomorrow_weather_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_tomorrow_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="@dimen/normalSize" />
<TextView
android:id="@+id/tv_tomorrow_temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:textColor="@color/white"
android:textSize="@dimen/normalSize" />
<TextView
android:id="@+id/tv_tomorrow_weatherName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:textColor="@color/white"
android:textSize="@dimen/normalSize" />
<TextView
android:id="@+id/tv_tomorrow_PM2_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:textColor="@color/white"
android:textSize="@dimen/normalSize"
android:visibility="gone" />
</LinearLayout>
<!-- 后天的天气 -->
<LinearLayout
android:id="@+id/dayaftertomorrow_weather_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_dayaftertomorrow_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="@dimen/normalSize" />
<TextView
android:id="@+id/tv_dayaftertomorrow_temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:textColor="@color/white"
android:textSize="@dimen/normalSize" />
<TextView
android:id="@+id/tv_dayaftertomorrow_weatherName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:textColor="@color/white"
android:textSize="@dimen/normalSize" />
<TextView
android:id="@+id/tv_dayaftertomorrow_PM2_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:textColor="@color/white"
android:textSize="@dimen/normalSize"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
今天的文章和风天气获取天气相关数据的方法_查看实时天气用什么软件比较好[通俗易懂]分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/86505.html