做一个简单的天气预报,首先想的就是联网权限,还有对“和风天气” 服务器的HTTP请求

 <uses-permission android:name="android.permission.INTERNET" />

先在和风天气(百度搜索)上注册一个账号 并且激活然后登陆,在“官网里”


在控制台里 获取自己的 key

获取JSON数据 的结构

“网址”+”?key”+”自己的秘钥”+”&location=”

接下里我们就需要在xml里 布局了

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.myapplication13.MainActivity"><EditText
        android:hint="请输入城市名称"android:id="@+id/main_editView"android:layout_width="match_parent"android:layout_height="50dp" /><Button
        android:id="@+id/main_button"android:layout_width="match_parent"android:layout_height="40dp"android:text="查询" /><RelativeLayout
        android:layout_marginTop="15dp"android:layout_width="match_parent"android:layout_height="40dp"><TextView
            android:id="@+id/main11"android:layout_width="80dp"android:layout_height="40dp"android:text="天气:"android:textSize="25sp" /><TextView
            android:id="@+id/main_tianqi"android:layout_width="wrap_content"android:layout_height="40dp"android:layout_toRightOf="@id/main11"android:textSize="25dp" /></RelativeLayout><RelativeLayout
        android:layout_marginTop="15dp"android:layout_width="match_parent"android:layout_height="40dp"><TextView
            android:id="@+id/main22"android:layout_width="80dp"android:layout_height="40dp"android:text="温度:"android:textSize="25sp" /><TextView
            android:id="@+id/main_wendu"android:layout_width="wrap_content"android:layout_height="40dp"android:layout_toRightOf="@id/main22"android:textSize="25dp" /></RelativeLayout><RelativeLayout
        android:layout_marginTop="15dp"android:layout_width="match_parent"android:layout_height="40dp"><TextView
            android:id="@+id/main33"android:layout_width="80dp"android:layout_height="40dp"android:text="风力:"android:textSize="25sp" /><TextView
            android:id="@+id/main_fengli"android:layout_width="wrap_content"android:layout_height="40dp"android:layout_toRightOf="@id/main33"```android:textSize="25dp" /></RelativeLayout></LinearLayout>

实时效果praview:

在java代码里 我们用AsynTask 来对HTTPURLConnection 和JSONObject 进行了属性 返回 传值 调用

package com.example.myapplication13;import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;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.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;public class MainActivity extends AppCompatActivity {private EditText editText;private Button button;private TextView tv_tianqi;private TextView tv_wendu;private TextView tv_fengli;private String API = "https://free-api.heweather.com/s6/weather/now?key=11e895a6b3854f0fb49508eea65df6ca&location=";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);binID();button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {String city = editText.getText().toString();new MyWeather().execute(API + city);}});}class MyWeather extends AsyncTask<String, String, String> {@Overrideprotected 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();}@Overrideprotected 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 basic = object1.getJSONObject("basic");JSONObject now = object1.getJSONObject("now");String city = basic.getString("location");String tianqi = now.getString("cond_txt");String wendu = now.getString("tmp");String fengli = now.getString("wind_dir");String qiangdu = now.getString("wind_sc");tv_tianqi.setText("今天是" + "“" + tianqi + "”" + "哦,主人");tv_wendu.setText(wendu + "℃");tv_fengli.setText(fengli + qiangdu + "级");} catch (JSONException e) {e.printStackTrace();}}}private void binID() {editText = findViewById(R.id.main_editView);button = findViewById(R.id.main_button);tv_tianqi = findViewById(R.id.main_tianqi);tv_fengli = findViewById(R.id.main_fengli);tv_wendu = findViewById(R.id.main_wendu);}
}

运行后 简单的天气软件就做出来啦。 不过我的模拟器问题。只能由拼音代替城市喽

和风天气 (简易版)相关推荐

  1. 和风天气V7获取不到数据问题

    前几天和风天气S6版开始不稳定,经常获取不到数据.搞了半天原来是和风已经放弃6版本的维护,开始强推7,反正我是这么理解的.有新的了,旧的就不让你用了.我想当然的以为把代码随便改一下就可以了.结果折腾了 ...

  2. Android使用高德与和风天气实现定位实况数据显示 简易新闻(十)

    Android使用高德与和风天气实现定位实况数据显示 简易新闻(十) 关于 效果图 第一步,添加和风引用 第二步,修改AndroidManifest.xml 第三步,修改activity_main.x ...

  3. indesign照片放入太大_猪肚鸡做法复杂,量太大,那来个简易版,用上大厨教的妙招更香了...

    [猪肚鸡做法复杂,量太大,那来个简易版,用上大厨教的妙招更香了]天气越来越冷了,今天准备炖鸡汤,正好老妈给了小半个已经处理好的猪肚,所以决定炖个简易版胡椒猪肚鸡.说实话外面的胡椒猪肚鸡做法是比较复杂的 ...

  4. Android开发之和风天气篇:1、获取天气信息

    TIME:2020年7月6日 :1.获取天气信息 和风天气API使用方法: step1 申请天气接口 step2 天气接口初始化 step3 选择服务模式 step4 通过URL请求数据 step5解 ...

  5. domoticz添加和风天气,让domoticz显示天气信息

    1.domoticz添加天气信息 domoticz中可以很方便的通过dark sky api显示天气信息,但是dark sky的信息不太准确,想添加国内天气的信息,网上的都是通过彩云的api获取jso ...

  6. 抖音微信消息推送情侣告白浪漫(简易版)

    抖音微信消息推送情侣告白浪漫(简易版) 一.首先去微信公众平台用微信扫码登录 登录后会自动生成属于自己的appId,appSecret 二.在上述登录后页面中下拉,用一个手机扫码会生成user_id( ...

  7. Android和风天气sdk

    Android使用和风天气Sdk获取实况天气数据(一) 说明(遇到的坑) 效果图 实现过程 第一步,注册账号(2018年以后的开发者,如果是以前的想知道如何获取实况天气也可以私我) 第二步 添加引用 ...

  8. 和风天气API 版本6和版本7

    这段时间和风天气apiV6版本天气预报一直有问题,我之前还专门写了一个分析,不过那个是个案,其他人可能不是这样,而且 实际上V6版本的数据获取是有问题,直接在网页上没问题,但用8266直接获取有时候就 ...

  9. 树莓派天气预报基于Python(相关支持 : 和风天气 备注 : 初级应用)

    1. 相关链接 1. 和风天气 API 开发文档 : https://dev.qweather.com/docs/api/ 2. 和风天气开发平台 : https://dev.qweather.com ...

最新文章

  1. 在Linux上安装PostgreSQL
  2. TextView的跑马灯效果实现
  3. 从零开始山寨Caffe·壹:仰望星空与脚踏实地
  4. 重设忘记的Mysql密码
  5. 整理的一些Android开发类免费视频课程
  6. linux时间格式怎么写,linux基础--时间格式
  7. 如何使用弱网环境来验证游戏中的一些延迟问题
  8. (24)HTTP 方法:GET 对比 POST
  9. 网页模板快速建站工具_自助建站相对传统建站有什么优势 - 建站极速通
  10. [AX]AX2012 C#使用IIS宿主AIF服务的一些问题
  11. java读加密脚本_尝试将wlst脚本嵌入到java类中时发生加密错误
  12. mysql sql
  13. opencv物体识别-识别水果
  14. 银联云闪付产品分析报告
  15. //利用 DirectoryInfo递归遍历文件夹,删除所有文件,文件夹
  16. 主存/内存/外存 区分
  17. 「Python」Mac下pip出现command not found和Could not find a version that satisfies the requirement ...解决方式
  18. 2019年9月 黑马头条项目14天
  19. 利益交错-HTML5视频标准之争
  20. ctf从零到入门0x04:(转载)ctf中最全的(脑洞大开的加密方法)

热门文章

  1. Verilog基础知识3(门控时钟及FPGA时钟使能处理)
  2. 炽热如初 向新而生|ISC2022 HackingClub白帽峰会圆满举办
  3. InfluxDB使用教程:数据库管理工具InfluxDBStudio
  4. 史上最全量化交易资源整理
  5. B2B商业模式以及交易模式
  6. 打印文件,打印机出error纸张
  7. xshell6 评估期已过 解决办法
  8. 苹果云服务器里的家人共享位置,iPhone“家人共享”服务是什么?
  9. 错误:is quoted with which must be escaped when used within the value
  10. (转)Serializable 序列化和反序列化