和风天气 (简易版)
2018-03-15 13:33
309 查看
做一个简单的天气预报,首先想的就是联网权限,还有对“和风天气” 服务器的HTTP请求
先在和风天气(百度搜索)上注册一个账号 并且激活然后登陆,在“官网里”
在控制台里 获取自己的 key
获取JSON数据 的结构
“网址”+”?key”+”自己的秘钥”+”&location=”
接下里我们就需要在xml里 布局了
实时效果praview:
在java代码里 我们用AsynTask 来对HTTPURLConnection 和JSONObject 进行了属性 返回 传值 调用
运行后 简单的天气软件就做出来啦。 不过我的模拟器问题。只能由拼音代替城市喽
<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="; @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(); new MyWeather().execute(API + city); } }); } 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 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); } }
运行后 简单的天气软件就做出来啦。 不过我的模拟器问题。只能由拼音代替城市喽
相关文章推荐
- Http请求和Json解析结合实例 (简易的天气预报系统)
- RxAndroid 应用(三)(简易天气App之Observable内存泄露)
- Android中 GsonFormat插件解析Jason 数据+和风天气接口解析案例
- python requests练手——和风天气api
- 简易天气预报代码
- 调用和风天气和百度语音合成接口,实现在浏览器中播报实时气象情况
- 天气预报,MYSQL之JDBC查询模块实现(代码)和简易插入模块实现(代码)以及PREPAREDSTATEMENT插入模块实现(代码)
- iOS开发:简易天气预报
- 简易天气预报
- ios开发 简易的天气应用(GET、XML)
- Android 实现简单当前时间点的天气情况(和风天气接口)
- RxAndroid 应用(四)(简易天气App之RxBus)
- Android开发之使用和风天气API遇到的问题及解决
- 使用weather.com.cn数据实现的简易天气demo
- 和风天气城市代码数据库
- 和风天气(一)数据分析
- 编写简易Android天气应用的代码示例
- 简易天气隐私策略
- Android实践之简易天气(一)
- [Android] 滴答天气(和风天气接口)