怎样用android制作一个天气预报APP及代码实现
时间: 2023-06-14 11:07:41 浏览: 224
安卓天气预报源码(带换肤功能)weatherApp.7z
5星 · 资源好评率100%
要制作一个天气预报APP,你需要掌握以下技能:
1. 掌握 Android Studio 的基本使用方法
2. 了解 Android 的基本布局和UI设计
3. 掌握网络请求与解析JSON数据
4. 掌握 Android 中的ListView、RecyclerView等控件
5. 掌握 Android 中的SharedPreferences存储
以下是一个简单的天气预报APP的代码实现,供你参考:
1. 布局文件(activity_main.xml)
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/city_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="城市名称"
android:textSize="24sp" />
<TextView
android:id="@+id/weather_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="天气信息"
android:textSize="18sp" />
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
```
2. Java 代码(MainActivity.java)
```
public class MainActivity extends AppCompatActivity {
private TextView cityName;
private TextView weatherInfo;
private ListView listView;
private List<String> forecastList = new ArrayList<>();
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cityName = findViewById(R.id.city_name);
weatherInfo = findViewById(R.id.weather_info);
listView = findViewById(R.id.list_view);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, forecastList);
listView.setAdapter(adapter);
String city = "北京"; // 待查询的城市名称
queryWeather(city);
}
private void queryWeather(String city) {
String url = "http://wthrcdn.etouch.cn/weather_mini?city=" + city;
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseData = response.body().string();
try {
JSONObject jsonObject = new JSONObject(responseData);
JSONObject data = jsonObject.getJSONObject("data");
final String city = data.getString("city");
final String weather = data.getString("wendu") + "℃ " + data.getString("ganmao");
JSONArray forecast = data.getJSONArray("forecast");
runOnUiThread(new Runnable() {
@Override
public void run() {
cityName.setText(city);
weatherInfo.setText(weather);
}
});
for (int i = 0; i < forecast.length(); i++) {
JSONObject item = forecast.getJSONObject(i);
String date = item.getString("date");
String high = item.getString("high");
String low = item.getString("low");
String type = item.getString("type");
final String forecastStr = date + " " + type + " " + low + "~" + high;
runOnUiThread(new Runnable() {
@Override
public void run() {
forecastList.add(forecastStr);
adapter.notifyDataSetChanged();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
```
以上代码只是一个简单的示例,实际制作中还需要考虑到错误处理、用户交互等方面。
阅读全文