android实现一个使用APL的能够自己输入城市查看天气预报的代码和布局文件
时间: 2024-02-19 11:01:38 浏览: 28
好的,以下是一个使用 APL (AccuWeatherAPI) 的 Android 天气预报应用的代码和布局文件。
首先,在 AndroidManifest.xml 文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.INTERNET" />
```
接着,在 build.gradle 文件中添加以下依赖:
```groovy
dependencies {
implementation 'com.android.volley:volley:1.2.0'
}
```
然后,在布局文件中添加以下代码:
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/cityEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入城市名" />
<Button
android:id="@+id/searchButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查看天气" />
<TextView
android:id="@+id/weatherTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="等待查询结果..." />
</LinearLayout>
```
现在,我们来编写代码。在 MainActivity.java 文件中,我们需要先获取用户输入的城市名,并将其转化为 APL 所需的格式。代码如下:
```java
public class MainActivity extends AppCompatActivity {
private EditText cityEditText;
private Button searchButton;
private TextView weatherTextView;
private final String API_KEY = "YOUR_API_KEY";
private final String API_URL = "http://dataservice.accuweather.com/currentconditions/v1/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cityEditText = findViewById(R.id.cityEditText);
searchButton = findViewById(R.id.searchButton);
weatherTextView = findViewById(R.id.weatherTextView);
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String city = cityEditText.getText().toString();
if (!city.isEmpty()) {
String url = API_URL + getCityKey(city) + "?apikey=" + API_KEY;
getWeather(url);
}
}
});
}
private String getCityKey(String city) {
// 将城市名转化为 API 所需的格式
return "search?apikey=" + API_KEY + "&q=" + city;
}
private void getWeather(String url) {
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
// 解析 JSON 数据并显示天气信息
JSONArray array = new JSONArray(response);
JSONObject object = array.getJSONObject(0);
String text = object.getString("WeatherText");
String temperature = object.getJSONObject("Temperature")
.getJSONObject("Metric").getString("Value");
weatherTextView.setText(text + ", " + temperature + "℃");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// 显示错误信息
weatherTextView.setText("查询失败,错误信息:" + error.getMessage());
}
});
queue.add(stringRequest);
}
}
```
其中,getCityKey() 函数将城市名转化为 API 所需的格式,并返回对应的城市 Key。getWeather() 函数使用 Volley 库发送 HTTP GET 请求,获取天气数据,并解析 JSON 数据。如果查询成功,将天气信息显示在 weatherTextView 上;如果查询失败,将错误信息显示在 weatherTextView 上。
最后,将 YOUR_API_KEY 替换为你的 APL API Key,运行应用即可进行天气查询。