页面绘制
问题1、下拉框需要背景为透明
我懒得写全部省份就写了5个所以不需要往下
图标准备
iconfont-阿里巴巴矢量图标库几坤年没来这了好怀念啊,图标库选择下雨的图标等
准备网络请求
0、API接口准备
api免费七日天气接口API 未来一周天气预报api (tianqiapi.com)
1、 添加网络请求权限
2、网络请求工具类
package com.example.weather.Unil;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Util {
//ctrl+shift+u全部大写/小写
public static final String URL_WEATHER_WITH="http://v1.yiketianqi.com/free/week?unescape=1&appid=00000000&appsecret=00000";
public static String doGet(String urlStr){
String result="";
InputStreamReader inputStreamReader = null;
//链接网路
HttpURLConnection connection=null;
BufferedReader bufferedReader=null;
try {
URL url = new URL(urlStr);
connection =(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
//从连接中读取数据
InputStream inputStream = connection.getInputStream();
//二进制转换(数据流加工)
inputStreamReader = new InputStreamReader(inputStream);
bufferedReader = new BufferedReader(inputStreamReader);
String line="";
StringBuilder stringBu=new StringBuilder();
while((line = bufferedReader.readLine())!=null){
//读取一行拼接
stringBu.append(line);
}
result=stringBu.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}finally {
//关闭流们
if (connection!=null)
connection.disconnect();
if (inputStreamReader!=null) {
try {
inputStreamReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (bufferedReader!=null) {
try {
bufferedReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
return result;
}
public static String getWeatherOfCity(String city){
//拼接天气url
String weatherUrl=URL_WEATHER_WITH+"&city"+city;
Log.d("gh", "-------------"+weatherUrl);
String weatherResult = doGet(weatherUrl);
Log.d("gh", "-------------"+weatherResult);
return weatherResult;
}
}
3、main中调用
4、查看日志成功
数据解析并渲染(JSON)
package com.example.weather;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatSpinner;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.RecyclerView;
import com.example.weather.Unil.Util;
import com.example.weather.bean.DayWeather;
import com.example.weather.bean.WeatherBean;
import com.google.gson.Gson;
public class MainActivity extends AppCompatActivity {
private AppCompatSpinner mSpinner;
private ArrayAdapter<String> mspAdapter;
private String[] mCities;
private TextView tvWendu,tvxiangxi,tvkongqi,tvWindPos,tvWindSpeed;
private ImageView imageView;
private RecyclerView rlFutureWeather;
private Handler myHandler=new Handler(Looper.myLooper()){
@Override
public void handleMessage(@NonNull Message msg) {
//ctrl+o重写方法
//传递给父
super.handleMessage(msg);
if (msg.what==0){
String weather=(String) msg.obj;
Gson gson = new Gson();
//解析完成
WeatherBean weatherBean = gson.fromJson(weather, WeatherBean.class);
updateUiOfWeather(weatherBean);
// Log.e("gh", "-----------------------"+weatherBean.toString());
}
}
};
private void updateUiOfWeather(WeatherBean weatherBean) {
if (weatherBean==null)
return;
DayWeather toDayWeather = weatherBean.getData().get(0);
if (toDayWeather==null)
return;
tvWendu.setText((Integer.parseInt(toDayWeather.getTemDay())+Integer.parseInt(toDayWeather.getTemNight()))/2+"");
tvxiangxi.setText(toDayWeather.getWea()+' '+"最高"+toDayWeather.getTemDay()+"° "+"最低"+toDayWeather.getTemNight()+"°");
tvkongqi.setText(Integer.parseInt(toDayWeather.getTemDay())+20+"");
tvWindPos.setText(toDayWeather.getWin());
tvWindSpeed.setText(toDayWeather.getWinSpeed());
getImgResOfWeather(toDayWeather.getWeaImg());
}
private void getImgResOfWeather(String weaStr) {
// 固定9种类型(您也可以根据wea字段自己处理):
// xue、lei、shachen、wu、yun、yu、yin、qing
int result=0;
switch (weaStr){
case "qing":result=R.drawable.qingtian;break;
case "yun":result=R.drawable.duoyun;break;
case "shachen":result=R.drawable.shanchenbao;break;
case "wu":result=R.drawable.wutian;break;
case "xue":result=R.drawable.xuetian;break;
case "yin":result=R.drawable.yintian;break;
case "yu":result=R.drawable.yutian;break;
case "lei":result=R.drawable.yutian;break;
}
imageView.setImageResource(result);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
initView();
}
//这里需要用下来关联自定义的布局怎么关联呢
//需要Adapter
private void initView(){
mSpinner = findViewById(R.id.spinner);
mCities=getResources().getStringArray(R.array.cities);//关联到下列边表
mspAdapter = new ArrayAdapter<>(this, R.layout.spitem,mCities);//
mSpinner.setAdapter(mspAdapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//选中城市后会开始获取天气
String selectCity=mCities[position];
getWeatherOfCity(selectCity);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
tvWendu=findViewById(R.id.wendu);
tvkongqi=findViewById(R.id.kongqi);
tvxiangxi=findViewById(R.id.juti);
tvWindPos=findViewById(R.id.windpos);
tvWindSpeed=findViewById(R.id.windSpeed);
imageView=findViewById(R.id.imageView);
rlFutureWeather=findViewById(R.id.scrollView2);
}
private void getWeatherOfCity(String selectCity) {
//开启子线程请求与网络(必须再子线程)
new Thread(new Runnable() {
@Override
public void run() {
//请求网络
String weatherResult = Util.getWeatherOfCity(selectCity);
// Log.e("gh", "-----------------------"+selectCity);
//使用handle将数据传递给主线程
//从消息池获取消息效率更高
Message msg= Message.obtain();
msg.what=0;
msg.obj=weatherResult;
myHandler.sendMessage(msg);
}
}).start();
}
}