我正在尝试使用Volley在我的应用中实现登录功能。我正在连接到localhost并执行脚本以从数据库中获取数据。使用日志,我发现执行永远不会进入onResponse
对象的Response.Listener
部分。
以下是代码:
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
Log.d(TAG, "onResponse()");
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
boolean email_found = jsonResponse.getBoolean("email_found");
if (success) {
Fragment fragment = getFragmentManager().findFragmentByTag(MainActivity.OPTIONS_TAG);
if (fragment == null) {
fragment = new LoginFragment();
}
getFragmentManager().beginTransaction().replace(R.id.container, fragment, MainActivity.OPTIONS_TAG).commit();
Toast.makeText(getContext(), "Successfull login", Toast.LENGTH_SHORT);
} else if (email_found){
mPasswordView.setError(getString(R.string.error_incorrect_password));
mPasswordView.requestFocus();
} else {
mEmailView.setError(getString(R.string.error_invalid_email));
mEmailView.requestFocus();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
Log.d(TAG, "LoginRequest");
LoginRequest loginRequest = new LoginRequest(email,password, responseListener);
LoginRequest
类:
class LoginRequest extends StringRequest {
private static final String LOGIN_REQUEST_URL = "http://localhost/myportal/login.php";
private Map <String, String> params;
public LoginRequest (String email, String password, Response.Listener<String> listener) {
super(Request.Method.POST, LOGIN_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("email", email);
params.put("password", password);
}
public Map<String, String> getParams() {
return params;
}
}
PHP脚本:
<?php
$con = mysqli_connect("localhost", "root", "" , "myportal");
$email = $_POST["email"];
$password = $_POST["password"];
$response = array();
$response["email_found"]=false;
$statement = mysqli_prepare($con, "SELECT * FROM user WHERE email = ?");
mysqli_stmt_bind_param($statement, "s", $email);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
if (mysqli_stmt_fetch($statement)){
$response["email_found"]=true;
}
$statement = mysqli_prepare($con, "SELECT * FROM user WHERE email = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $email, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $name, $lastname, $email, $password, $year);
$response["success"] = false;
while (mysqli_stmt_fetch($statement)) {
$response["success"] = true;
$response["name"] = $name;
$response["lastname"] = $lastname;
$response["email"] = $email;
$response["password"] = $password;
$response["year"] = $year;
}
echo json_encode($response);
?>
Logcat输出:
05-24 21:29:05.812 2114-2496/com.google.android.gms E/Volley: [133] BasicNetwork.performRequest: Unexpected response code 307 for https://android.googleapis.com/nova/herrevad/network_quality_info
05-24 21:29:05.812 2114-17798/com.google.android.gms W/RmtRprtRfrshSvc: [19078] RemoteReportsRefreshService.a: java.util.concurrent.ExecutionException: com.android.volley.ServerError
java.util.concurrent.ExecutionException: com.android.volley.ServerError
at com.android.volley.toolbox.RequestFuture.doGet(SourceFile:117)
at com.android.volley.toolbox.RequestFuture.get(SourceFile:88)
at com.google.android.gms.herrevad.services.RemoteReportsRefreshService.a(SourceFile:134)
at com.google.android.gms.herrevad.services.RemoteReportsRefreshService.a(SourceFile:89)
at com.google.android.gms.gcm.au.run(SourceFile:140)
Caused by: com.android.volley.ServerError
at com.android.volley.toolbox.BasicNetwork.performRequest(SourceFile:163)
at com.google.android.gms.common.server.v.performRequest(SourceFile:64)
at com.android.volley.NetworkDispatcher.run(SourceFile:112)
05-24 21:29:05.946 1532-2199/system_process I/AccountManagerService: getTypesVisibleToCaller: isPermitted? true
05-24 21:29:05.947 2114-17806/com.google.android.gms E/ProtoReq: [19079] a.a: want to send authenticated request, but no Google account on device
05-24 21:29:18.207 2114-18125/com.google.android.gms W/BaseAppContext: Using Auth Proxy for data requests.
答案 0 :(得分:0)
所以解决方法是将IP更改为适当的IP并添加我忘记的RequestQueue。
RequestQueue queue = Volley.newRequestQueue(getContext());
queue.add(loginRequest);