用android studio写一个天气预报安卓软件,接入中国天气网获取天气数据,使用阿拉伯数字实时显示温度,提供一个完整的代码,并且可以运行
时间: 2023-04-10 07:05:15 浏览: 154
一个基于Android studio开发的简易天气预报APP源码
5星 · 资源好评率100%
非常感谢您的提问。以下是一个简单的天气预报安卓应用程序的代码,它使用中国天气网提供的API来获取天气数据,并使用阿拉伯数字实时显示温度。请注意,这只是一个示例代码,您需要根据您的需求进行修改和定制。
MainActivity.java
```
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private EditText cityEditText;
private Button searchButton;
private TextView temperatureTextView;
private static final String API_KEY = "YOUR_API_KEY";
private static final String API_URL = "http://t.weather.itboy.net/api/weather/city/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cityEditText = findViewById(R.id.cityEditText);
searchButton = findViewById(R.id.searchButton);
temperatureTextView = findViewById(R.id.temperatureTextView);
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String city = cityEditText.getText().toString();
if (!city.isEmpty()) {
new WeatherTask().execute(city);
}
}
});
}
private class WeatherTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String city = params[0];
String apiUrl = API_URL + city;
try {
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
connection.disconnect();
return response.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
if (result != null) {
try {
JSONObject jsonObject = new JSONObject(result);
JSONObject dataObject = jsonObject.getJSONObject("data");
String temperature = dataObject.getString("wendu");
temperatureTextView.setText(temperature + "℃");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}
```
activity_main.xml
```
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<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="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/cityEditText"
android:text="搜索" />
<TextView
android:id="@+id/temperatureTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/searchButton"
android:textSize="48sp"
android:textAlignment="center" />
</RelativeLayout>
```
请注意,您需要将YOUR_API_KEY替换为您在中国天气网注册的API密钥。此外,您还需要在AndroidManifest.xml文件中添加以下权限:
```
<uses-permission android:name="android.permission.INTERNET" />
```
希望这可以帮助您开始编写您自己的天气应用程序。
阅读全文