我正在使用Retrofit2从MySQL数据库中获取数据列表。我想将所有获取的数据放入android的RecyclerView中,但响应不成功。我起初是Call<Loan>
,它返回了一个响应,但是我的适配器仅接受列表数组,因此我将其更改为Call<List<Loan>>
,但是现在响应失败。我做了一个邮递员请求,它返回了数据,所以我不知道我在哪里做错了。
fetchLoans方法
public void fetchLoans(String key){
final Call<List<Loan>> loanCall = ApiClient
.getInstance()
.getApi()
.fetchLoans(key);
loanCall.enqueue(new Callback<List<Loan>>() {
@Override
public void onResponse(Call<List<Loan>> call, Response<List<Loan>> response) {
if (response.isSuccessful()) {
if (response.body() != null) {
Log.i("onSuccess", response.body().toString());
progressBar.setVisibility(View.GONE);
List<Loan> loanList = response.body();
loanAdapter = new LoanAdapter(context, loanList);
recyclerView.setAdapter(loanAdapter);
loanAdapter.notifyDataSetChanged();
} else {
progressBar.setVisibility(View.GONE);
Toast.makeText(context, "No loans at the moment" + response.body().toString(), Toast.LENGTH_SHORT).show();
}
}else{
progressBar.setVisibility(View.GONE);
Toast.makeText(context, "Request failed", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<List<Loan>> call, Throwable t) {
progressBar.setVisibility(View.GONE);
Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
我的API方法
@GET("Loans/fetch_loan.php?")
Call<List<Loan>> fetchLoans(
@Query("keyWord") String keyWord);
我的POJO班
public class Loan {
@SerializedName("ID") private int ID;
@SerializedName("admID") private int admID;
@SerializedName("fullName") private String fullName;
@SerializedName("contactNo") private String contactNo;
@SerializedName("borrowedAmt") private double borrowedAmt;
@SerializedName("borrowedDate") private String borrowedDate;
@SerializedName("interestRate") private String interestRate;
@SerializedName("interest") private double interest;
@SerializedName("paymentType") private String paymentType;
@SerializedName("message") private String message;
@SerializedName("success") private Boolean success;
public void setID(int ID) {
this.ID = ID;
}
public void setAdmID(int admID) {
this.admID = admID;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
public void setBorrowedAmt(double borrowedAmt) {
this.borrowedAmt = borrowedAmt;
}
public void setBorrowedDate(String borrowedDate) {
this.borrowedDate = borrowedDate;
}
public void setInterestRate(String interestRate) {
this.interestRate = interestRate;
}
public void setInterest(double interest) {
this.interest = interest;
}
public void setPaymentType(String paymentType) {
this.paymentType = paymentType;
}
public String getMessage() {
return message;
}
public Boolean getSuccess() {
return success;
}
public int getID() {
return ID;
}
public int getAdmID() {
return admID;
}
public String getFullName() {
return fullName;
}
public String getContactNo() {
return contactNo;
}
public double getBorrowedAmt() {
return borrowedAmt;
}
public String getBorrowedDate() {
return borrowedDate;
}
public String getInterestRate() {
return interestRate;
}
public double getInterest() {
return interest;
}
public String getPaymentType() {
return paymentType;
}
}
在邮递员中进行请求时返回的内容。
Fetchloan request
最佳答案
我发现网址有所不同。在您的API方法中,它是Loans/fetch_loan.php?
,在您的邮递员场景中,它是Loans/fetchLoan.php?
。
如果是真的,那肯定不会得到成功的回应。或将您的API方法URL更改为@GET("Loans/fetchLoan.php?")
。 (用大写L代替下划线)
同样,您也无需在API方法的URL末尾使用?
。 Retrofit的@Query
在内部添加它。
记录错误消息而不是仅仅举杯是一个很好的习惯。喜欢
Log.e("TAG", "Error: ", t);