gpt4 book ai didi

java - 如何在Retrofit2中查询

转载 作者:行者123 更新时间:2023-12-02 11:38:37 24 4
gpt4 key购买 nike

我将获取我登录用户的数据。因此,不要获取所有数据并将其存储在 ArrayList 中,然后调用循环,然后通过 if 语句进行检查。有没有最简单、最短的方法?顺便说一句,这是我的代码。

public static final String BASE_URL = "http://busartery.us-3.evennode.com/";

public static Retrofit getClient() {
Retrofit retrofit = null;

if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(Config.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}

public interface APIServiceDriver {

@POST("/sessions/")
@FormUrlEncoded
Call<Driver> driverLogin(@Field("username") String username,
@Field("password") String password,
@Field("type") String type);

@GET("/drivers/")
Call<List<Driver>> getDriverInfo();
}

APIServiceDriver serviceDriver = Config.getClient().create(APIServiceDriver.class);
Call<List<Driver>> call = serviceDriver.getDriverInfo();
call.enqueue(new Callback<List<Driver>>() {
@Override
public void onResponse(Call<List<Driver>> call, Response<List<Driver>> response) {
if (response.isSuccessful()) {
driverList = response.body();

if (driverList.size() != 0) {
for (int i = 0; i < driverList.size(); i++) {
mDriverName = driverList.get(i).getDriversName();
mUsername = driverList.get(i).getDriversName();
mAge = driverList.get(i).getAge();
mBusNumber = driverList.get(i).getBusNumber();
mBusType = driverList.get(i).getBusType();
mCompanyName = driverList.get(i).getCompanyName();
mRoute = driverList.get(i).getRoute();
mCapacity = driverList.get(i).getCapacity();

if (mUsername.equals(pref.getString("username", ""))) {
txtDriverName.setText(mDriverName);
txtUsername.setText(mUsername);
txtAge.setText(mAge);
txtBusNumber.setText(mBusNumber);
txtBusType.setText(mBusType);
txtCompanyName.setText(mCompanyName);
txtRoute.setText(mRoute);
txtCapacity.setText(mCapacity);
}
}
}
}
}

@Override
public void onFailure(Call<List<Driver>> call, Throwable t) {
progressDialog.dismiss();
Toast.makeText(DriverInformationActivity.this, "onFailure: "+t.getMessage(), Toast.LENGTH_SHORT).show();
}
});

驱动程序POJO

public class Driver {

@SerializedName("fullName")
@Expose
private String fullName;
@SerializedName("username")
@Expose
private String username;
@SerializedName("password")
@Expose
private String password;
@SerializedName("age")
@Expose
private Integer age;
@SerializedName("employeeId")
@Expose
private String employeeId;
@SerializedName("isActive")
@Expose
private Boolean isActive;
@SerializedName("longitude")
@Expose
private String longitude;
@SerializedName("latitude")
@Expose
private String latitude;
@SerializedName("origin")
@Expose
private String origin;
@SerializedName("estimatedTime")
@Expose
private String estimatedTime;
@SerializedName("busNumber")
@Expose
private BusNumber busNumber;

public String getFullName() {
return fullName;
}

public void setFullName(String fullName) {
this.fullName = fullName;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getEmployeeId() {
return employeeId;
}

public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}

public Boolean getIsActive() {
return isActive;
}

public void setIsActive(Boolean isActive) {
this.isActive = isActive;
}

public String getLongitude() {
return longitude;
}

public void setLongitude(String longitude) {
this.longitude = longitude;
}

public String getLatitude() {
return latitude;
}

public void setLatitude(String latitude) {
this.latitude = latitude;
}

public String getOrigin() {
return origin;
}

public void setOrigin(String origin) {
this.origin = origin;
}

public String getEstimatedTime() {
return estimatedTime;
}

public void setEstimatedTime(String estimatedTime) {
this.estimatedTime = estimatedTime;
}

public BusNumber getBusNumber() {
return busNumber;
}

public void setBusNumber(BusNumber busNumber) {
this.busNumber = busNumber;
}}

巴士号码 POJO

public class BusNumber {

@SerializedName("companyName")
@Expose
private String companyName;
@SerializedName("busNumber")
@Expose
private String busNumber;
@SerializedName("busType")
@Expose
private String busType;
@SerializedName("destination")
@Expose
private String destination;
@SerializedName("location")
@Expose
private String location;
@SerializedName("capacity")
@Expose
private Integer capacity;
@SerializedName("route")
@Expose
private String route;

public String getCompanyName() {
return companyName;
}

public void setCompanyName(String companyName) {
this.companyName = companyName;
}

public String getBusNumber() {
return busNumber;
}

public void setBusNumber(String busNumber) {
this.busNumber = busNumber;
}

public String getBusType() {
return busType;
}

public void setBusType(String busType) {
this.busType = busType;
}

public String getDestination() {
return destination;
}

public void setDestination(String destination) {
this.destination = destination;
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}

public Integer getCapacity() {
return capacity;
}

public void setCapacity(Integer capacity) {
this.capacity = capacity;
}

public String getRoute() {
return route;
}

public void setRoute(String route) {
this.route = route;
}}

而且,此代码将被定向到 onFailure()。

Database

最佳答案

这样使用

@GET("/drivers/")
Call<List<Driver>> getDriverInfo(@Query("driver_id") int driverId);

关于java - 如何在Retrofit2中查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48738985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com