androidstudio天气预报程序黑马程序员
时间: 2023-11-09 15:08:46 浏览: 97
android天气预报程序
4星 · 用户满意度95%
以下是一个简单的天气预报程序的代码:
1. 创建一个空的 Android Studio 项目,然后在项目的 build.gradle 文件中添加以下依赖:
```
dependencies {
implementation 'com.android.volley:volley:1.1.1'
}
```
2. 在项目的 layout 文件夹下创建一个名为 activity_main.xml 的布局文件,添加以下代码:
```
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/city_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入城市名称"
android:inputType="text"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:id="@+id/search_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询"
app:layout_constraintTop_toBottomOf="@id/city_input"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<TextView
android:id="@+id/result_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="@id/search_btn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
```
3. 在项目的 Java 文件夹下创建一个名为 MainActivity.java 的 Java 文件,添加以下代码:
```
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
private EditText cityInput;
private Button searchBtn;
private TextView resultText;
private RequestQueue requestQueue;
private String apiUrl = "http://apis.juhe.cn/simpleWeather/query";
private String apiKey = "你的API Key";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cityInput = findViewById(R.id.city_input);
searchBtn = findViewById(R.id.search_btn);
resultText = findViewById(R.id.result_text);
requestQueue = Volley.newRequestQueue(this);
searchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String city = cityInput.getText().toString();
if (city.isEmpty()) {
Toast.makeText(MainActivity.this, "城市名称不能为空", Toast.LENGTH_SHORT).show();
} else {
searchWeather(city);
}
}
});
}
private void searchWeather(String city) {
String url = apiUrl + "?city=" + city + "&key=" + apiKey;
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
int errorCode = response.getInt("error_code");
if (errorCode == 0) {
JSONObject result = response.getJSONObject("result");
JSONArray future = result.getJSONArray("future");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < future.length(); i++) {
JSONObject item = future.getJSONObject(i);
String date = item.getString("date");
String temperature = item.getString("temperature");
String weather = item.getString("weather");
builder.append(date).append(" ").append(temperature).append(" ").append(weather).append("\n");
}
resultText.setText(builder.toString());
} else {
String reason = response.getString("reason");
Toast.makeText(MainActivity.this, reason, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "查询天气失败", Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(request);
}
}
```
4. 将 apiKey 替换为你的 API Key。
5. 运行程序,输入城市名称并点击查询按钮,即可查询到该城市未来几天的天气情况。
阅读全文