我的第一个帖子:Anything I try to store on database enters with value '0'
我解决了这个问题,但现在我遇到了另一个问题: 我尝试登录时为什么会出现此错误?
09-11 17:20:46.382 2577-5031/com.example.gustavo.loginregister D/NetworkSecurityConfig: No Network Security Config specified, using platform default
09-11 17:20:47.415 2577-5031/com.example.gustavo.loginregister E/Volley: [145] BasicNetwork.performRequest: Unexpected response code 404 for https://xxxxxxx.000webhostapp.com/Login.php
09-11 17:22:39.640 2577-6700/com.example.gustavo.loginregister E/Volley: [152] BasicNetwork.performRequest: Unexpected response code 404 for https://xxxxxxx.000webhostapp.com/Login.php
LoginActivity.java文件:
package com.example.gustavo.loginregister;
import android.app.AlertDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final EditText edtEmail = (EditText) findViewById(R.id.edtEmail);
final EditText edtSenha = (EditText) findViewById(R.id.edtSenha);
final Button btnLogin = (Button) findViewById(R.id.btnLogin);
final TextView txtCadastreSe = (TextView) findViewById(R.id.txtCadastreSe);
txtCadastreSe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent registroIntent = new Intent(LoginActivity.this, RegisterActivity.class);
LoginActivity.this.startActivity(registroIntent);
}
});
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String email = edtEmail.getText().toString();
final String password = edtSenha.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
String name = jsonResponse.getString("name");
String lastname = jsonResponse.getString("lastname");
Intent intent = new Intent(LoginActivity.this, UserAreaActivity.class);
intent.putExtra("name", name);
intent.putExtra("lastname", lastname);
intent.putExtra("email", email);
LoginActivity.this.startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setMessage("Login Failed")
.setNegativeButton("Retry",null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
LoginRequest loginRequest = new LoginRequest(email, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
queue.add(loginRequest);
}
});
}
}
LoginRequest.java文件:
package com.example.gustavo.loginregister;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
public class LoginRequest extends StringRequest {
private static final String LOGIN_REQUEST_URL="https://wavecheck.000webhostapp.com/Login.php";
private Map<String, String> params;
public LoginRequest(String email, String password, Response.Listener<String> listener){
super(Method.POST, LOGIN_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("email", email);
params.put("password", password);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
Login.php文件:
<?php
require("Password.php");
$con = mysqli_connect("localhost", "xxx", "xxx", "xxxx");
$email = $_POST["email"];
$password = $_POST["password"];
$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);
mysqli_stmt_bind_result($statement, $colUserID, $colName, $colLastname, $colEmail, $colPassword);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)){
if (password_verify($password, $colPassword)) {
$response["success"] = true;
$response["name"] = $colName;
$response["lastname"] = $colLastname;
$response["email"] = $colEmail;
}
}
echo json_encode($response);
?>
答案 0 :(得分:0)
直接访问https://wavecheck.000webhostapp.com/Login.php时,它不会返回JSON。它返回404错误页面“此处没有任何内容。 这个域已准备好注册。“。这是来自主机的消息。显然你正在使用免费主机,并且”wavecheck“子域不存在。所以你需要重新托管你的PHP脚本,或与免费主持人联系,弄清楚发生了什么。