【发布时间】:2019-02-05 14:47:53
【问题描述】:
我一直在试图找出这个错误在哪里,但找不到。有没有ibe可以帮我查一下?
谢谢家人
这是我的 php 代码
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$id = $_POST['id'];
$password = $_POST['password'];
require_once 'connect.php';
$sql = "SELECT * FROM students_table WHERE id = '$id'";
$response = mysqli_query ($conn, $sql);
$result = array();
$result['login'] = array();
if (mysqli_num_rows($response) === 1){
$row = mysqli_fetch_assoc($response);
if(password_verify($password, $row['password'])){
$index['id'] = $row['id'];
$index['email'] = $row['email'];
array_push($result['login'], $index);
$result['success'] = "1";
$result['message'] = "success";
echo json_encode($result);
mysqli_close($conn);
}else {
$result['success'] = "0";
$result['message'] = "error";
echo json_encode($result);
mysqli_close($conn);
}
}
header('Content-Type: application/json');
}
?>
这是我的 Android 工作室代码
public void Login(final String idField, final String passwordField){
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_LOGIN, new Response.Listener<String>() {
@Override
public void onResponse(java.lang.String response) {
try {
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success");
JSONArray jsonArray = jsonObject.getJSONArray("login");
Log.d("JSONR", jsonObject.toString());
if (success.equals("1")){
for (int i = 0; i<jsonArray.length(); i++){
JSONObject object = jsonArray.getJSONObject(i);
String email = object.getString("email").trim();
Toast.makeText(LoginActivity.this, "Logged In. \n"+email, Toast.LENGTH_LONG).show();
//Access WelcomePage Page if credentials are authenticated
startActivity(new Intent(LoginActivity.this, WelcomePage.class));
}
}
}catch (JSONException e) {
e.printStackTrace();
Toast.makeText(LoginActivity.this, "Error" + e.toString(), Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(LoginActivity.this, "Error" + error.toString(), Toast.LENGTH_LONG).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("id", idField);
params.put("password", passwordField);
return params;
}
};
//manage network requests using the volley request queue. I'm creating a request queue and passing in request objects
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
我一直在试图找出错误的来源,但没有得到它。 有什么帮助吗?
【问题讨论】:
-
你能打印响应,看看那里的价值是什么???
-
看不到任何响应,除了:2019-02-05 15:26:01.166 13106-13106/com.example.inductionbuddy W/System.err: org.json.JSONException: Value Connected
-
因此响应包含一些 HTML 标记,例如显示
-
是我的 php 文件中的错误吗? @Android 杀手
-
是的,来自 PHP 的响应是错误的。它应该以 JSON 格式的字符串发送响应,以便它可以轻松地转换为 JSON 对象。但它是以html格式发送响应,它可能是一些错误或正确的响应,但它不应该是html格式。