【问题标题】:Got a JSONException: End of input at character 0 of得到一个 JSONException: End of input at character 0 of
【发布时间】:2015-11-18 08:34:57
【问题描述】:

我正在尝试按照在线教程创建此登录名,但收到此错误。我在本地主机上试过这个,但它在服务器上不起作用。谁能告诉我我的错误是什么。这是我的代码:

private void checkLogin(final String email, final String password) {
        // Tag used to cancel the request
        String tag_string_req = "req_login";

        pDialog.setMessage("Logging in ...");
        showDialog();

        StringRequest strReq = new StringRequest(Method.POST,
                AppConfig.URL_LOGIN, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Login Response: " + response.toString());
                hideDialog();

                try {
                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");

                    // Check for error node in json
                    if (!error) {
                        // user successfully logged in
                        // Create login session
                        session.setLogin(true);

                        // Now store the user in SQLite
                        String uid = jObj.getString("uid");

                        JSONObject user = jObj.getJSONObject("user");
                        String name = user.getString("name");
                        String email = user.getString("email");
                        String created_at = user
                                .getString("created_at");

                        // Inserting row in users table
                        db.addUser(name, email, uid, created_at);

                        // Launch main activity
                        Intent intent = new Intent(LoginActivity.this,
                                MainActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        // Error in login. Get the error message
                        String errorMsg = jObj.getString("error_msg");
                        Toast.makeText(getApplicationContext(),
                                errorMsg, Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    // JSON error
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                }

            }

【问题讨论】:

  • 暂时注释掉onResponse里面的所有行,只留下Log.d(TAG, "Login Response: " + response);然后发布你的新logcat信息
  • 不是添加图片,而是添加有问题的日志。
  • 在我注释完所有行之后,它就一直在登录。 Logcat: D/RegisterActivity: Login Response:
  • 你的响应为空,没有收到任何字符串
  • 你能告诉我我应该如何纠正这个问题吗?我已经在 localhost 上尝试过了,它成功了。

标签: java android json android-volley


【解决方案1】:

这意味着要么响应不是 JSON 格式,要么客户端根本没有得到任何响应。尝试以下步骤。: - 1.在应用程序中使用 URL 之前,请在 Web 浏览器中检查您是否获得所需的响应。如果有任何服务器端错误,它将显示在 Web 浏览器中。 2. 现在使用 JSON 验证器检查服务端响应,以检查响应是否为有效 JSON 3.如果您的服务器端是,则使用 logcat 或 toast 消息打印您的响应并检查响应。

【讨论】:

    【解决方案2】:

    我刚刚找到了解决它的方法。导致问题的是 DB_Functions.php 文件中的代码。我已经稍微更改了代码,然后它现在可以工作了。非常感谢你们的帮助。我还附上了代码,以防有人遇到同样的问题。祝大家好运

    public function getUserByEmailAndPassword($email, $password) {
       $result = mysqli_query($this->conn,"SELECT * FROM users WHERE email = '$email'") or die(mysqli_connect_errno());
    
    // check for result
    
       $no_of_rows = mysqli_num_rows($result);
    
       if ($no_of_rows > 0) {
          $result = mysqli_fetch_array($result);
          $salt = $result['salt'];
          $encrypted_password = $result['encrypted_password'];
          $hash = $this->checkhashSSHA($salt, $password);
    

    【讨论】:

      猜你喜欢
      • 2018-10-14
      • 1970-01-01
      • 1970-01-01
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-08
      • 1970-01-01
      相关资源
      最近更新 更多