错误org.json.JSONException:在字符0的输入结束

时间:2018-09-20 05:24:44

标签: php android json database

我的登录代码有什么问题,当我注册时它可以正常工作,并且用户可以成功注册,但是当我尝试登录时,我收到错误消息“输入字符0的结尾”

这是我的Login.php代码

<?php
include "connect.php";
if ($_SERVER['REQUEST_METHOD']=='POST') {

    $email = $_POST['email'];
    $password = $_POST['password'];

    $sql = "SELECT * FROM User WHERE email='$email' ";

    $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['name'] = $row['name'];
            $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);

        }
    }

}
?>

这是我在android studio(Login.java)中的登录代码

package com.m99.ml99.everyday;

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.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

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

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 {

private EditText email, password;
private Button btn_login;
private TextView link_regist;
private ProgressBar loading;
private static String URL_LOGIN= 
"https://hanymina43.000webhostapp.com/Login.php";


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

    loading = findViewById(R.id.loading);
    email = findViewById(R.id.email);
    password = findViewById(R.id.password);
    btn_login = findViewById(R.id.btn_login);
    link_regist = findViewById(R.id.link_regist);

    btn_login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String mEmail = email.getText().toString().trim();
            String mPass = password.getText().toString().trim();

            if (!mEmail.isEmpty() || !mPass.isEmpty()) {
                Login(mEmail, mPass);
            } else {
                email.setError("Please insert email");
                password.setError("Please insert password");
            }
        }
    });

    link_regist.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(Login.this, Register.class));
        }
    });

}

private void Login(final String email, final String password) {

    loading.setVisibility(View.VISIBLE);
    btn_login.setVisibility(View.GONE);

    StringRequest stringRequest = new StringRequest(Request.Method.POST, 
URL_LOGIN,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        String success = jsonObject.getString("success");
                           JSONArray jsonArray =
jsonObject.getJSONArray("login");

                        if (success.equals("1")) {

                            for (int i = 0; i < jsonArray.length(); i++) {

                                JSONObject object = 
jsonArray.getJSONObject(i);

                                String name = 
  object.getString("name").trim();
                                String email = 
  object.getString("email").trim();
                                loading.setVisibility(View.GONE);


                            }

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                        loading.setVisibility(View.GONE);
                        btn_login.setVisibility(View.VISIBLE);
                        Toast.makeText(Login.this, "Error " +e.toString(), 
Toast.LENGTH_SHORT).show();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    loading.setVisibility(View.GONE);
                    btn_login.setVisibility(View.VISIBLE);
                    Toast.makeText(Login.this, "Error " +error.toString(), 
Toast.LENGTH_SHORT).show();
                }
            })
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("email", email);
            params.put("password", password);
            return params;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

}
}

有人可以告诉我我的代码在哪里出错了,我不认为这是服务器,因为在同一台服务器上运行寄存器,并且工作正常,谢谢。

1 个答案:

答案 0 :(得分:0)

new JSONObject调用之前检查从服务器接收到的JSON格式。

如果它是有效的JSON,则在您的问题中发布从API收到的输出。