繁体   English   中英

使用Retrofit2拨打电话时未收到任何回应

[英]Receiving no response when I do a call using retrofit2

我正在使用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;
}

}

在邮递员中进行请求时返回的内容。

提取请求

我发现网址有所不同。 在您的API方法中,它是Loans/fetch_loan.php? 您的邮递员sceenshot中的哪里是Loans/fetchLoan.php?

如果是真的,那肯定不会得到成功的回应。 或将您的API方法URL更改为@GET("Loans/fetchLoan.php?") (用大写L代替下划线

而且您不需要使用? 在API方法中网址的末尾。 改造的@Query在内部添加它。

记录错误消息而不是仅仅举杯是一个很好的习惯。 喜欢

Log.e("TAG", "Error: ", t);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM