Skip to content

Commit f959496

Browse files
committed
新增丨天气预报Demo 简单版
1 parent c21e1a7 commit f959496

File tree

165 files changed

+20105
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

165 files changed

+20105
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88

99
### Android相关
1010

11-
### [新增]百度地图定位
11+
### [新增]天气预报|简单版
12+
13+
1. 天气预报Demo [WeatherDemo.zip](https://gitee.com/littlecurl/AppProjects/raw/master/WeatherDemo.zip)
14+
15+
### 百度地图定位
1216

1317
1. 仅仅百度地图定位Demo [JustBaiDuMapLocationDemo.zip](https://gitee.com/littlecurl/AppProjects/raw/master/JustBaiDuMapLocationDemo.zip)
1418

WeatherDemo.zip

1.56 MB
Binary file not shown.

WeatherDemo/.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties

WeatherDemo/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

WeatherDemo/app/build.gradle

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
plugins {
2+
id 'com.android.application'
3+
}
4+
5+
android {
6+
compileSdkVersion rootProject.ext.compileSdkVersion
7+
8+
defaultConfig {
9+
applicationId "com.example.weatherdemo"
10+
minSdkVersion rootProject.ext.minSdkVersion
11+
targetSdkVersion rootProject.ext.targetSdkVersion
12+
versionName rootProject.ext.versionName
13+
versionCode rootProject.ext.versionCode
14+
15+
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
compileOptions {
26+
sourceCompatibility JavaVersion.VERSION_1_8
27+
targetCompatibility JavaVersion.VERSION_1_8
28+
}
29+
}
30+
31+
dependencies {
32+
33+
implementation 'androidx.appcompat:appcompat:1.2.0'
34+
implementation 'com.google.android.material:material:1.3.0'
35+
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
36+
testImplementation 'junit:junit:4.+'
37+
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
38+
39+
implementation project(":library-xhttp2")
40+
implementation project(":library-EditSpiner")
41+
}

WeatherDemo/app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.example.weatherdemo;
2+
3+
import android.content.Context;
4+
5+
import androidx.test.platform.app.InstrumentationRegistry;
6+
import androidx.test.ext.junit.runners.AndroidJUnit4;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
* Instrumented test, which will execute on an Android device.
15+
*
16+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
17+
*/
18+
@RunWith(AndroidJUnit4.class)
19+
public class ExampleInstrumentedTest {
20+
@Test
21+
public void useAppContext() {
22+
// Context of the app under test.
23+
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24+
assertEquals("com.example.weatherdemo", appContext.getPackageName());
25+
}
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.weatherdemo">
4+
5+
<application
6+
android:name="MyApplication"
7+
android:allowBackup="true"
8+
android:icon="@mipmap/ic_launcher"
9+
android:label="@string/app_name"
10+
android:roundIcon="@mipmap/ic_launcher_round"
11+
android:supportsRtl="true"
12+
android:networkSecurityConfig="@xml/network_security_config"
13+
android:theme="@style/Theme.WeatherDemo">
14+
<activity
15+
android:name=".MainActivity"
16+
android:exported="true">
17+
<intent-filter>
18+
<action android:name="android.intent.action.MAIN" />
19+
20+
<category android:name="android.intent.category.LAUNCHER" />
21+
</intent-filter>
22+
</activity>
23+
</application>
24+
25+
</manifest>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.example.weatherdemo;
2+
3+
import android.os.SystemClock;
4+
5+
/**
6+
* author : Android 轮子哥
7+
* github : https://github.com/getActivity/AndroidProject
8+
* time : 2018/10/18
9+
* desc : 防双击判断工具类
10+
*/
11+
public final class DoubleClickHelper {
12+
13+
/** 数组的长度为2代表只记录双击操作 */
14+
private static final long[] TIME_ARRAY = new long[2];
15+
16+
/**
17+
* 是否在短时间内进行了双击操作
18+
*/
19+
public static boolean isOnDoubleClick() {
20+
// 默认间隔时长
21+
return isOnDoubleClick(1500);
22+
}
23+
24+
/**
25+
* 是否在短时间内进行了双击操作
26+
*/
27+
public static boolean isOnDoubleClick(int time) {
28+
System.arraycopy(TIME_ARRAY, 1, TIME_ARRAY, 0, TIME_ARRAY.length - 1);
29+
TIME_ARRAY[TIME_ARRAY.length - 1] = SystemClock.uptimeMillis();
30+
return TIME_ARRAY[0] >= (SystemClock.uptimeMillis() - time);
31+
}
32+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.example.weatherdemo;
2+
3+
import androidx.appcompat.app.AppCompatActivity;
4+
5+
import android.annotation.SuppressLint;
6+
import android.os.Bundle;
7+
import android.text.TextUtils;
8+
import android.view.View;
9+
import android.widget.Button;
10+
import android.widget.TextView;
11+
import android.widget.Toast;
12+
13+
import com.example.weatherdemo.bean.Body;
14+
import com.example.weatherdemo.bean.Forecast;
15+
import com.example.weatherdemo.bean.ResponseData;
16+
import com.xuexiang.xhttp2.XHttp;
17+
import com.xuexiang.xhttp2.callback.CallBackProxy;
18+
import com.xuexiang.xhttp2.callback.SimpleCallBack;
19+
import com.xuexiang.xhttp2.exception.ApiException;
20+
21+
import java.util.Arrays;
22+
import java.util.List;
23+
24+
import cn.edu.heuet.editspinner.EditSpinner;
25+
26+
public class MainActivity extends AppCompatActivity {
27+
private String cityName;
28+
private TextView tvContent;
29+
30+
@Override
31+
protected void onCreate(Bundle savedInstanceState) {
32+
super.onCreate(savedInstanceState);
33+
setContentView(R.layout.activity_main);
34+
// 1、读取 XML 数据
35+
String[] cityCodeArray = getResources().getStringArray(R.array.cityCode);
36+
37+
String[] cityNameArray = getResources().getStringArray(R.array.cityName);
38+
List<String> cityNameList = Arrays.asList(cityNameArray);
39+
40+
// 2、将数据填充给 Spinner 控件
41+
EditSpinner editSpinner = findViewById(R.id.editspinner);
42+
editSpinner.setItemData(cityNameList);
43+
44+
tvContent = findViewById(R.id.tv_content);
45+
Button btSearch = findViewById(R.id.bt_search);
46+
47+
// 3、按钮点击响应事件
48+
btSearch.setOnClickListener(new View.OnClickListener() {
49+
50+
@Override
51+
public void onClick(View v) {
52+
// 防止快速点击
53+
if (DoubleClickHelper.isOnDoubleClick(3000)) {
54+
Toast.makeText(MainActivity.this, "查询太频繁,手别抖!", Toast.LENGTH_SHORT).show();
55+
return;
56+
}
57+
cityName = editSpinner.getText();
58+
if (TextUtils.isEmpty(cityName)) {
59+
return;
60+
}
61+
// 根据填写的城市名称查到对应的城市代码
62+
int index = cityNameList.indexOf(cityName);
63+
if (index == -1) {
64+
Toast.makeText(MainActivity.this, "输入城市名称未录入系统,请重新输入", Toast.LENGTH_SHORT).show();
65+
return;
66+
}
67+
68+
String url = "http://t.weather.itboy.net/api/weather/city/" + cityCodeArray[index];
69+
// Xhttp2 请求网络
70+
XHttp.get(url)
71+
.syncRequest(false)
72+
.execute(new CallBackProxy<Body<ResponseData>, ResponseData>(
73+
new SimpleCallBack<ResponseData>() {
74+
@Override
75+
public void onSuccess(ResponseData data) {
76+
// 处理 data
77+
dealWithData(data);
78+
}
79+
80+
@Override
81+
public void onError(ApiException e) {
82+
// 处理异常
83+
}
84+
}
85+
) {
86+
});//最后一定要有 {} 否则解析失败 作者:为中华之崛起而敲代码 https://www.bilibili.com/read/cv8476993?spm_id_from=333.999.0.0 出处:bilibili
87+
88+
}
89+
});
90+
}
91+
92+
@SuppressLint("SetTextI18n")
93+
private void dealWithData(ResponseData data) {
94+
if (data == null) {
95+
Toast.makeText(MainActivity.this, "结果异常!", Toast.LENGTH_SHORT).show();
96+
return;
97+
}
98+
Forecast todayForecast = data.getForecast().get(0);
99+
String today = buildForecastString(todayForecast, "今天");
100+
101+
Forecast tomorrowForecast = data.getForecast().get(1);
102+
String tomorrow = buildForecastString(tomorrowForecast, "明天");
103+
104+
tvContent.setText(today + tomorrow);
105+
}
106+
107+
private String buildForecastString(Forecast forecast, String day) {
108+
return day + "【" + cityName + "】天气" + "\n" +
109+
"日期:" + forecast.getYmd() + "丨" +
110+
forecast.getWeek() + ";\n" +
111+
"天气类型:" + forecast.getType() + ";\n" +
112+
"最高温度:" + forecast.getHigh() + ";\n" +
113+
"最低温度:" + forecast.getLow() + ";\n" +
114+
"刮风情况:" + forecast.getFl() + "丨" +
115+
forecast.getFx() +
116+
";\n" +
117+
"天气建议:" + forecast.getNotice() + ";\n\n\n";
118+
}
119+
120+
}

0 commit comments

Comments
 (0)