gpt4 book ai didi

php - 无法登录 Android 应用程序。错误 : org. json.JSONException : Value
转载 作者:行者123 更新时间:2023-11-29 18:26:20 25 4
gpt4 key购买 nike

我正在创建一个连接到本地主机的简单 Android 应用程序。我已成功创建注册页面,应用程序可以毫无问题地注册新用户。我正在使用 Volley 库来创建注册/登录。

问题出在登录功能上。当我填写登录详细信息并单击“登录”时,出现错误

09-11 13:59:16.856 16107-16107/com.example.virus.bloodpressurereader W/System.err: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray
09-11 13:59:16.856 16107-16107/com.example.virus.bloodpressurereader W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
09-11 13:59:16.856 16107-16107/com.example.virus.bloodpressurereader W/System.err: at org.json.JSONArray.<init>(JSONArray.java:96)
09-11 13:59:16.856 16107-16107/com.example.virus.bloodpressurereader W/System.err: at org.json.JSONArray.<init>(JSONArray.java:108)
09-11 13:59:16.856 16107-16107/com.example.virus.bloodpressurereader W/System.err: at com.example.virus.bloodpressurereader.Login$1$1.onResponse(Login.java:71)
09-11 13:59:16.856 16107-16107/com.example.virus.bloodpressurereader W/System.err: at com.example.virus.bloodpressurereader.Login$1$1.onResponse(Login.java:65)
09-11 13:59:16.856 16107-16107/com.example.virus.bloodpressurereader W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
09-11 13:59:16.856 16107-16107/com.example.virus.bloodpressurereader W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
09-11 13:59:16.856 16107-16107/com.example.virus.bloodpressurereader W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
09-11 13:59:16.856 16107-16107/com.example.virus.bloodpressurereader W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
09-11 13:59:16.856 16107-16107/com.example.virus.bloodpressurereader W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
09-11 13:59:16.856 16107-16107/com.example.virus.bloodpressurereader W/System.err: at android.os.Looper.loop(Looper.java:148)
09-11 13:59:16.856 16107-16107/com.example.virus.bloodpressurereader W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
09-11 13:59:16.856 16107-16107/com.example.virus.bloodpressurereader W/System.err: at java.lang.reflect.Method.invoke(Native Method)
09-11 13:59:16.856 16107-16107/com.example.virus.bloodpressurereader W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
09-11 13:59:16.856 16107-16107/com.example.virus.bloodpressurereader W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我的login.php文件是这个

<?php
require "conn.php";

$user_email = $_POST["userEmail"];
$user_password = $_POST["userPass"];

$sql = "select name, email from user_profile where email = '$user_email' and password = '$user_password'";

$result = mysqli_query($conn, $sql);

$response = array();

if(mysqli_num_rows($result) > 0){

$row = mysqli_fetch_row($result);
$name = $row[0];
$email = $row[1];
$code = "login success";

array_push($response, array("code"=>$code, "name"=>$name, "email"=>$email));

echo json_encode($response);


}else {

$code = "login failed";
$message = "user not found.. Try again";

array_push($response, array("code"=>$code,"message"=>$message));

echo json_encode($response);
}
mysqli_close($conn);
?>

我的login.java文件是这个

package com.example.virus.bloodpressurereader;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.MainThread;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class Login extends AppCompatActivity {

EditText email, password;
Button loginUser;

String userEmail, userPassword;

String login_url = "http://192.168.0.144/login.php";

AlertDialog.Builder builder;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

builder = new AlertDialog.Builder(Login.this);

loginUser = (Button)findViewById(R.id.login_button);
email = (EditText) findViewById(R.id.login_email);
password = (EditText)findViewById(R.id.login_password);

loginUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//check conditions for user email and password
userEmail = email.getText().toString();
userPassword = password.getText().toString();

if(userEmail.equals("") || userPassword.equals("")){

builder.setTitle("Something went wrong");
displayAlert("Enter a valid email and password");
}else {
//authenticate from server
StringRequest stringRequest = new StringRequest(Request.Method.POST, login_url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//handle response from server

try {
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String code = jsonObject.getString("code");

if (code.equals("login failed")){
builder.setTitle("Login error");
displayAlert(jsonObject.getString("message"));
}else {
Intent i = new Intent(Login.this, MainActivity.class);
startActivity(i);
}

} catch (JSONException e) {
e.printStackTrace();
}


}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Login.this, "Error", Toast.LENGTH_SHORT).show();
error.printStackTrace();

}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {

Map<String, String> params = new HashMap<String, String>();
params.put("email", userEmail);
params.put("password", userPassword);
return params;
}
};
MySingleton.getInstance(Login.this).addToRequestque(stringRequest);
}

}
});

}
//display alert
public void displayAlert(String message){
builder.setMessage(message);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
email.setText("");
password.setText("");
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}


//registration link
public void registerUser(View view){
Intent i = new Intent(this, Register.class);
startActivity(i);
}

}

如果我没有在登录页面中输入任何内容并单击登录,则会显示正确的警报,但是,一旦输入电子邮件和密码并单击登录,我就会收到上述错误。

我的 PHP 脚本工作正常,因为我创建了一个虚拟的 login.html 并对其进行了测试。在这种情况下我得到了正确的响应。

最佳答案

(代表 OP 发布解决方案)

改变

$user_email = $_POST["userEmail"]; 
$user_password = $_POST["userPass"];

$user_email = $_POST["email"]; 
$user_password = $_POST["password"];

关于php - 无法登录 Android 应用程序。错误 : org. json.JSONException : Value <br of type java. lang.String 无法转换为 JSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46157706/

25 4 0

Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com