简单的天气预报实现android项目代码
时间: 2023-10-30 20:09:15 浏览: 61
以下是一个简单的天气预报实现Android项目的代码示例:
1. activity_main.xml
```
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/cityTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="City Name"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
<ImageView
android:id="@+id/weatherImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/cityTextView"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
<TextView
android:id="@+id/temperatureTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:text="Temperature"
android:layout_below="@id/weatherImageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
</RelativeLayout>
```
2. MainActivity.java
```
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private TextView cityTextView;
private ImageView weatherImageView;
private TextView temperatureTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cityTextView = findViewById(R.id.cityTextView);
weatherImageView = findViewById(R.id.weatherImageView);
temperatureTextView = findViewById(R.id.temperatureTextView);
new WeatherTask().execute();
}
private class WeatherTask extends AsyncTask<Void, Void, Void> {
private String city;
private String temperature;
private int weatherIcon;
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("http://api.openweathermap.org/data/2.5/weather?q=Shanghai&appid={your_api_key}");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
String response = builder.toString();
JSONObject jsonObject = new JSONObject(response);
JSONObject mainObject = jsonObject.getJSONObject("main");
temperature = mainObject.getString("temp");
temperature = String.valueOf(Math.round(Float.parseFloat(temperature) - 273.15)) + "°C";
JSONObject weatherObject = jsonObject.getJSONArray("weather").getJSONObject(0);
weatherIcon = getResources().getIdentifier("ic_" + weatherObject.getString("icon"), "drawable", getPackageName());
city = jsonObject.getString("name");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
cityTextView.setText(city);
weatherImageView.setImageResource(weatherIcon);
temperatureTextView.setText(temperature);
}
}
}
```
注意:在上面的代码中,需要替换URL中的{your_api_key}为你在OpenWeatherMap网站上获取到的API密钥。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)