使用android studio 写一个简易的武汉实时公交程序并给出详细源码
时间: 2024-03-23 07:42:50 浏览: 175
好的,下面是一个简单的武汉实时公交程序的示例代码:
1. 首先,创建一个新的 Android Studio 项目,并添加以下依赖项:
```groovy
dependencies {
implementation 'com.android.volley:volley:1.2.0'
implementation 'com.google.code.gson:gson:2.8.6'
}
```
2. 在布局文件中添加一个 RecyclerView 和一个 ProgressBar:
```xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
```
3. 创建一个 BusAdapter 类,用于填充 RecyclerView:
```java
public class BusAdapter extends RecyclerView.Adapter<BusAdapter.BusViewHolder> {
private List<Bus> buses;
public BusAdapter() {
buses = new ArrayList<>();
}
public void setBuses(List<Bus> buses) {
this.buses = buses;
notifyDataSetChanged();
}
@NonNull
@Override
public BusViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bus, parent, false);
return new BusViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull BusViewHolder holder, int position) {
Bus bus = buses.get(position);
holder.route.setText(bus.getRoute());
holder.busNum.setText(bus.getBusNum());
holder.startTime.setText(bus.getStartTime());
holder.endTime.setText(bus.getEndTime());
holder.interval.setText(bus.getInterval());
}
@Override
public int getItemCount() {
return buses.size();
}
static class BusViewHolder extends RecyclerView.ViewHolder {
TextView route;
TextView busNum;
TextView startTime;
TextView endTime;
TextView interval;
public BusViewHolder(@NonNull View itemView) {
super(itemView);
route = itemView.findViewById(R.id.route);
busNum = itemView.findViewById(R.id.busNum);
startTime = itemView.findViewById(R.id.startTime);
endTime = itemView.findViewById(R.id.endTime);
interval = itemView.findViewById(R.id.interval);
}
}
}
```
4. 创建一个 Bus 类,用于存储公交信息:
```java
public class Bus {
private String route;
private String busNum;
private String startTime;
private String endTime;
private String interval;
public Bus(String route, String busNum, String startTime, String endTime, String interval) {
this.route = route;
this.busNum = busNum;
this.startTime = startTime;
this.endTime = endTime;
this.interval = interval;
}
public String getRoute() {
return route;
}
public String getBusNum() {
return busNum;
}
public String getStartTime() {
return startTime;
}
public String getEndTime() {
return endTime;
}
public String getInterval() {
return interval;
}
}
```
5. 创建一个 MainActivity 类,用于处理网络请求及其响应:
```java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private RecyclerView recyclerView;
private ProgressBar progressBar;
private BusAdapter busAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
progressBar = findViewById(R.id.progressBar);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
busAdapter = new BusAdapter();
recyclerView.setAdapter(busAdapter);
fetchData();
}
private void fetchData() {
String url = "http://bus.wuhancloud.cn:9087/realBusTime/getRealBusTimeByLine.action?lineId=1";
RequestQueue requestQueue = Volley.newRequestQueue(this);
progressBar.setVisibility(View.VISIBLE);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressBar.setVisibility(View.GONE);
List<Bus> buses = parseResponse(response);
busAdapter.setBuses(buses);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressBar.setVisibility(View.GONE);
Log.e(TAG, "onErrorResponse: ", error);
}
});
requestQueue.add(stringRequest);
}
private List<Bus> parseResponse(String response) {
List<Bus> buses = new ArrayList<>();
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String route = jsonObject.getString("route");
String busNum = jsonObject.getString("busNum");
String startTime = jsonObject.getString("startTime");
String endTime = jsonObject.getString("endTime");
String interval = jsonObject.getString("interval");
Bus bus = new Bus(route, busNum, startTime, endTime, interval);
buses.add(bus);
}
} catch (JSONException e) {
Log.e(TAG, "parseResponse: ", e);
}
return buses;
}
}
```
6. 最后,创建一个 item_bus.xml 布局文件,用于显示每个公交信息:
```xml
<TextView
android:id="@+id/route"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="路线: " />
<TextView
android:id="@+id/busNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="车牌号: " />
<TextView
android:id="@+id/startTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发车时间: " />
<TextView
android:id="@+id/endTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="末班时间: " />
<TextView
android:id="@+id/interval"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发车间隔: " />
```
这样,一个简单的武汉实时公交程序就完成了。当用户启动该应用程序时,它会发送一个网络请求,并将返回的 JSON 数据解析为 Bus 对象列表。然后,它将这些对象填充到 RecyclerView 中,以显示公交信息。
阅读全文