duanchen9594 2018-11-18 21:22
浏览 100

Errororg.json.JSONException:字符0的输入结束

I created an app which requires a log in system. I created the registration, it works, but when I want to login I got this error message

Errororg.json.JSONException: End of input at character 0 of

I gave Internet Permission!

My login Class is

package com.quizonwheels.quizfinal;

import android.content.Intent;
import android.net.Uri;
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 Button openMore;
 private TextView openRegister;
 private Button btn_login;
    private EditText email,password;
    private static String URL_LOGIN = "https://test-onaca.lapausenii.ro/login.php";
    private ProgressBar loading;


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

        openMore = findViewById(R.id.learnmore);
        openRegister = findViewById(R.id.openRegister);
        btn_login = findViewById(R.id.login);
        loading = findViewById(R.id.loading1);
        email = findViewById(R.id.login_email);
        password = findViewById(R.id.login_password);

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

        openMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("https://quizonwheels.com")));
            }
        });

        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 E-mail");
                    password.setError("Please insert Password");
                }
            }
        });
    }

    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 succes = jsonObject.getString("succes");
                    JSONArray jsonArray = jsonObject.getJSONArray("login");
                    if(succes.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();
                            Toast.makeText(Login.this, "Succes 
Your name:" +name+"
 Your Email:" +email, Toast.LENGTH_SHORT).show();
                            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);
    }
}

And my php code is:

<?php
  if($_SERVER['REQUEST_METHOD']== 'POST')
  {
      $email = $_POST['email'];
      $password = $_POST['password'];

      require_once 'connect.php';

      $sql = " SELECT * FROM users WHERE email = '$email'";
      $response = mysqli_query($conn,$sql);
      $result = array();
      $result['login']= array();
      if(mysqli_num_rows($respone)===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['succes'] = "1";
              $result['message']="succes";
              echo json_encode($result);

              mysqli_close($conn);

          }
          else {
              $result['succes']="0";
              $result['message']="error";
              echo json_encode($result);

              mysqli_close($conn);

          }
      }
  }
?>

I was looking on google to find the solution but I couldnt find one.

  • 写回答

1条回答 默认 最新

  • douya7282 2018-11-18 22:46
    关注

    My rep doesn't allow me to comment, so hopefully I'm allowed to share my thoughts like this. Your code only outputs some JSON if a given email address was found in the database.

    Are you sure you're feeding it an existing email address, with no duplicates? Also, consider offloading the hash verification to the RDBMS. This might speed up the script, and it result in a clearer separation of concerns. You'll also want to escape the user input you're feeding into the SQL, as mysqli_query doesn't do any escaping by default. A better approach might be to use PDO prepared statements.

    评论

报告相同问题?

悬赏问题

  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题