我要获取我登录用户的数据。因此,不是获取所有数据并存储在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;
}}
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()。
答案 0 :(得分:2)
使用这种方式
@GET("/drivers/")
Call<List<Driver>> getDriverInfo(@Query("driver_id") int driverId);
答案 1 :(得分:0)
您需要在BusNumber POJO
Arraylist中传递Driver POJO
。
替换此代码
@SerializedName("busNumber")
@Expose
private BusNumber busNumber;
要
@SerializedName("busNumber")
@Expose
private ArrayList<BusNumber> busNumber;
您的Setter
和Getter
方法
public ArrayList<BusNumber> getBusNumber() {
return busNumber;
}
public void setBusNumber(ArrayList<BusNumber> busNumber) {
this.busNumber = busNumber;
}