使用简单的 checkifUserAlreadyExist php 文件作为基础 Url 时,改造 onResponse 不会触发?

Retrofit onResponse not firing when using a simple checkifUserAlreadyExist php file as Base Url?

我正在开发这个简单的应用程序,它从 mysql 插入和检索数据,我正在使用 Retrofit 将 post 数据发送到 php 正在执行 sql 的文件] 像保存用户数据和检查用户是否已经存在这样的操作。 当我创建一个具有 his/her 凭据(如名称、phone 等)的用户对象时,它工作正常,但现在我试图只发送一个字符串数据,即 phone,我想检查是否phone 号码已存在于数据库中。 Call.enqueue 中根本没有调用 Retrofit onResponse 方法,我找不到任何原因。 Php 文件运行良好,因为我使用简单的 html 表单对其进行了测试。 知道我做错了什么。

这是我的代码。

ApiInterface.java

@FormUrlEncoded
@POST("checkUser.php")
Call<String> checkUser(
        @Field("phone") String phone
);

ApiClient.java

private static final String BASE_URL = "https://araincommunity.website/";
private static Retrofit retrofit;

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

主要代码:

public void userAlreadyExist(String phone) {
    apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
    Call<String> call = apiInterface.checkUser(phone);
    call.enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            alreadyRegistered = response.isSuccessful() && response.body() != null;
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            alreadyRegistered = false;
        }
    });
}

注意:alreadyRegistered 是一个全局布尔变量,在成功响应的情况下正在更新。

我认为发生的情况是您的回复未正确解析。您指出您期望:{"success":false,"message":"Error"},这是一个 JSON 对象,并且您正在使用 GsonConverterFactory,因此 Retrofit 期望一个与您的响应定义相匹配的实体。

我会试试这个:

创建一个 CheckUserResponse class,它看起来像这样:

class CheckUserResponse {

     @SerializedName("success") Boolean success;

     @SerializedName("message") String message;

}

然后在您的 API 调用定义中使用它,如下所示:

@FormUrlEncoded
@POST("checkUser.php")
Call<CheckUserResponse> checkUser(
    @Field("phone") String phone
);

您还需要更改下一个回调和调用引用才能使用此模型,但这应该可行。

希望对您有所帮助。

Luis Alonso Paulino Flores 提供的答案对我帮助很大。 我修改了我的代码并在这里分享它以防有人需要它。

ApiInterface.Java

@FormUrlEncoded
@POST("checkUser.php")
Call<CheckUser> checkUser(@Field("phone") String phone);

ApiClient.Java

private static final String BASE_URL = "https://araincommunity.website/";
private static Retrofit retrofit;

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

CheckUser.Java //响应模型Class

public class CheckUser {
@Expose
@SerializedName("response")
private String response;

public String getResponse() {
    return response;
}

public void setResponse(String response) {
    this.response = response;
}
}

主要代码:

public void userAlreadyExist(final String phone) {
    ApiInterface apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
    Call<CheckUser> call = apiInterface.checkUser(phone);
    call.enqueue(new Callback<CheckUser>() {
        @Override
        public void onResponse(Call<CheckUser> call, Response<CheckUser> response) {
            pbar.setVisibility(View.GONE);
            tvWait.setVisibility(View.GONE);
            etPhone.setEnabled(true);
            btnNext.setEnabled(true);
            Log.e("Rashid Faheem", "Response Code:" + response.code());
            if (response.isSuccessful()){
                if (response.body().getResponse().equalsIgnoreCase("ok")){
                    Intent it = new Intent(GetPhoneNumber.this, AlreadyDonor.class);
                    it.putExtra("phone", phone);
                    startActivity(it);
                }else{
                    Intent it = new Intent(GetPhoneNumber.this, GetName.class);
                    it.putExtra("phone", phone);
                    startActivity(it);

                }

            }
        }

        @Override
        public void onFailure(Call<CheckUser> call, Throwable t) {

        }
    });
}

CheckUser.php

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$phone=$_POST['phone'];
require_once("connect.php");
$query = "SELECT phone FROM bloodgroup WHERE phone = '$phone' ";
$response = mysqli_query($conn, $query);
$data = mysqli_num_rows($response);
if($data>0){  
$error='ok';
echo json_encode(array('response'=>$error));
}else{  
$error = 'failed';
echo json_encode(array('response'=>$error));    
}
}
?>