HTTPURL 和 JSON 创建萌版 天气报告(附代码)

xiaoxiao2021-02-28  26

当我们熟悉了HTTP请求,和JSON ;来创建一个天气报告来加深吧;

通过网页搜索 :和风天气 并且 注册一个用户 在官网里 开发选项中 选择API说明文档里 选择 普通用户 的网址; 在控制台中 可以看到我们的key 接下来 我们就试试网址的可读性 一般 这样的网址格式: 网址+now+key+&location

通过我的网址 在www.json.cn 里效果如图:

说明我的网址是有效的

开始创建我们的项目之前,对安卓 还要网络权限设置一下:

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

开始创建我们的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> <ImageView android:id="@+id/main_imageView" android:layout_gravity="center" android:layout_width="80dp" android:layout_height="80dp" /> </LinearLayout>

效果图:

在java代码里 我们在耗时操作里 进行两项的肯定,URL 和JSON;

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.ImageView; 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 ImageView imageView; 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); imageView=findViewById(R.id.main_imageView); } }

实现的效果:

转载请注明原文地址: https://www.6miu.com/read-2632341.html

最新回复(0)