由getValue引起的错误,它是检查查询的响应。邮递员给出了正确的答案,但在应用程序上启动时为空。
代码如下:
private void loggedin() {
FirebaseUser user = mAuth.getCurrentUser();
String id = user.getUid();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Base_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
LoginApi api = retrofit.create(LoginApi.class);
Call<Value> call = api.login(id);
call.enqueue(new Callback<Value>() {
@Override
public void onResponse(Call<Value> call, Response<Value> response) {
try {
String value = response.body().getValue();
if (value.equals("1")) {
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();
} else {
Intent intent = new Intent(Login.this, Register.class);
startActivity(intent);
finish();
}
}catch (Throwable throwable){
Toast.makeText(Login.this, "Error " + throwable, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Value> call, Throwable t) {
Toast.makeText(Login.this, "Error " + t, Toast.LENGTH_SHORT).show();
}
});
}
这是LoginAPI:
public interface LoginApi {
@FormUrlEncoded
@POST("/login.php")
Call<Value> login(@Field("id") String id);}
这是值代码:
@SerializedName("value")
private String value;
public String getValue() {
return value;
}
这是php代码:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$response = array();
$id = $_POST['id'];
$dbhost = 'localhost';
$dbuser = 'root'; // ini berlaku di xampp
$dbpass = ''; // ini berlaku di xampp
$dbname = 'ezparking';
$connect = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
$sql = "SELECT * FROM customer WHERE id_cust ='$id'";
$check = mysqli_fetch_array(mysqli_query($connect, $sql));
if (isset($check)) {
$response["value"] = 1;
echo json_encode($response);
} else {
$response["value"] = 0;
echo json_encode($response);
}
mysqli_close($connect);
}
这是邮递员的回复: Postman Response