Я использую залп, чтобы сделать запрос php для моих приложений для обработки функции входа в систему.
Я проверил, что файл php работает, но теперь я обнаружил, что в моих приложениях он не не будет ответа после нажатия кнопки входа в систему, даже сообщение об ошибке.
Я распечатал часть журнала и обнаружил, что код внутри конструктора получателя ответа не запускается, даже если система печатает в начале onResponse
Вот мой код:
public class LoginRequest extends StringRequest {
private static final String LOGIN_REQUEST_URL = "http://localhost:8080/login.php";
private Map<String, String> params;
public LoginRequest(String username, String password, Response.Listener<String> listener) {
super(Method.POST, LOGIN_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("username", username);
params.put("password", password);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
@Override
public void onClick (View v){
switch(v.getId()){
case R.id.btnLogin:
final String username = etUsername.getText().toString();
final String password = etPassword.getText().toString();
// Response received from the server
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println("Listener create");
try {
System.out.println("Get response");
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
String name = jsonResponse.getString("name");
Intent intent = new Intent(DriverLoginActivity.this, DriverMainPage.class);
intent.putExtra("name", name);
DriverLoginActivity.this.startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(DriverLoginActivity.this);
builder.setMessage("Login Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
LoginRequest loginRequest = new LoginRequest(username, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(DriverLoginActivity.this);
queue.add(loginRequest);
}
}