我正在发出GET请求:
public interface CheckUserInDBRequest {
@GET("api/checkUserInDB.php")
Call<ResponseBody> searchForUser(
@Query("login") String login,
@Query("pass") String pass
);
}
我的答案是真的|| json中的false,根据数据库中是否有用户。
Retrofit.Builder builder = new Retrofit.Builder().baseUrl("https://kurusa.zhecky.net/").addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
CheckUserInDBRequest client = retrofit.create(CheckUserInDBRequest.class);
Call<ResponseBody> call = client.searchForUser (
UserLogin.getText().toString(),
UserPass.getText().toString()
);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
Toast.makeText(MainActivity.this, response.body().toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
Toast.makeText(MainActivity.this, "no ", Toast.LENGTH_SHORT).show();
}
});
以下是okHttp3
的输出。我不明白如何得到正常答案。
答案 0 :(得分:2)
首先,创建一个代表JSON
对象
public class UserResult {
public boolean isSet;
}
以这种方式重组Retrofit
来电
@GET("api/checkUserInDB.php")
Call<UserResult> searchForUser(
@Query("login") String login,
@Query("pass") String pass
);
然后在你的代码中:
call.enqueue(new Callback<UserResult>() {
@Override
public void onResponse(@NonNull Call<UserResult> call, @NonNull Response<UserResult> response) {
if (response.isSuccesfull()) {
UserResult result = response.body();
//use the value result.isSet where you need it
} else {
//something is broken
}
}
@Override
public void onFailure(@NonNull Call<UserResult> call, @NonNull Throwable t) {
Toast.makeText(MainActivity.this, "no ", Toast.LENGTH_SHORT).show();
}
});