java - 使用 Retrofit2 调用电话时没有收到任何响应

标签 java retrofit2

我正在使用 Retrofit2 从 MySQL 数据库中获取数据列表。我想将所有获取的数据放入 android 中的 RecyclerView 但响应不成功。我做了Call<Loan>起初,它返回了一个响应,但我的适配器只接受列表数组,所以我将其更改为 Call<List<Loan>>但现在响应不成功。我做了一个 postman 请求,它返回了数据,所以我不知道我哪里做错了。

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;
}

}

当我在 Postman 中发出请求时,它会返回什么。

Fetchloan request

最佳答案

我发现网址有所不同。在您的 API 方法中,它是 Loans/fetch_loan.php?,在您的 Postman 场景中,它是 Loans/fetchLoan.php?

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

而且您也不需要在 API 方法中的 URL 末尾使用 ? 。 Retrofit 的 @Query 在内部添加它。

记录错误消息而不仅仅是 Toasting 始终是一个好习惯。喜欢

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

关于java - 使用 Retrofit2 调用电话时没有收到任何响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57183561/

相关文章:

kotlin - android/kotlin-在单击“后退”按钮时,使用CompositeDisposal(RXjava2)进行的 retrofit 无法转到响应/可抛出的 block

java - 使用 JSON 文件初始化静态类

java - 分析/追踪 JVM 中潜在的 native 内存泄漏

java - W/System.err : java.net.SocketException:套接字已关闭

android - 无法使所有模型的 Retrofit2 接口(interface)通用

java - 接口(interface)声明为NULL

android - RxJava2 如何在请求参数更改时更新现有订阅

android - Retrofit2 如何将/放在动态 baseUrl 的末尾?

java - Spring应用程序上下文重新加载不会更新由DelegatingFilterProxy注册的bean

java - 在 Firebase 中仅下载部分数据库引用?