java - Bug : E/JSON Parser﹕ Error parsing data org. json.JSONException : Value Hello of type java. lang.String无法转换为JSONObject

标签 java php android json

当我点击我的登录按钮时,我得到了这个,我不知道为什么,我不知道为什么它在错误中说你好,我搜索了我的整个项目,找不到“你好”的出现.此错误还有其他原因吗?

public class Login extends Activity implements OnClickListener {

    private EditText user, pass;
    private Button mSubmit, mRegister;

    // Progress Dialog
    private ProgressDialog pDialog;

    // JSON parser class
    JSONParser jsonParser = new JSONParser();




    private static final String LOGIN_URL = "http://xxxxxxxxxxxx";



    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        // setup input fields
        user = (EditText) findViewById(R.id.username);
        pass = (EditText) findViewById(R.id.password);

        // setup buttons
        mSubmit = (Button) findViewById(R.id.login);
        mRegister = (Button) findViewById(R.id.register);

        // register listeners
        mSubmit.setOnClickListener(this);
        mRegister.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.login:
                new AttemptLogin().execute();
                break;
            case R.id.register:
                Intent i = new Intent(this, Register.class);
                startActivity(i);
                break;

            default:
                break;
        }
    }

    class AttemptLogin extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Login.this);
            pDialog.setMessage("Attempting login...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {
            // TODO Auto-generated method stub
            // Check for success tag
            int success;
            String username = user.getText().toString();
            String password = pass.getText().toString();
            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("username", username));
                params.add(new BasicNameValuePair("password", password));

                Log.d("request!", "starting");
                // getting product details by making HTTP request
                JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
                        params);

                // check your log for json response
                Log.d("Login attempt", json.toString());

                // json success tag
                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    Log.d("Login Successful!", json.toString());
                    // save user data
                    SharedPreferences sp = PreferenceManager
                            .getDefaultSharedPreferences(Login.this);
                    Editor edit = sp.edit();
                    edit.putString("username", username);
                    edit.commit();

                    Intent i = new Intent(Login.this, MainActivity.class);
                    finish();
                    startActivity(i);
                    return json.getString(TAG_MESSAGE);
                } else {
                    Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                    return json.getString(TAG_MESSAGE);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;

        }

        protected void onPostExecute(String file_url) {
            // dismiss the dialog once product deleted
            pDialog.dismiss();
            if (file_url != null) {
                Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
            }

        }

    }

}

编辑代码

<?php
       if (!isset($_SESSION)) {
  session_start();
}


//load and connect to MySQL database stuff
require("config.inc.php");




if (!empty($_POST)) {
$query = "SELECT id,password, username, iv FROM users WHERE username = :username";




    $username = $_POST['username'];

    //encrypting the username and password to compare to the ones stored in the databaase



 $query_params = array(
        ':username' => $username,
    );

    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);


    }
    catch (PDOException $ex) {

        $response["success"] = 0;
        $response["message"] = "Database Error1. Please Try Again!";
        die("Failed to run query: " . $ex->getMessage());

    }




    //fetching all the rows from the query

        $password = $_POST['password'];

    $row = $stmt->fetch();
    $key = $row['iv'];
    $encrypted_password = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $secret_key, $password, MCRYPT_MODE_CBC, $key); 
 if ($row) {
        if ($encrypted_password == $row['password']) {


            $login_ok = true;
        }
    }

    if ($login_ok) {
        $response["success"] = 1;
        $response["message"] = "Login successful!";
        die(json_encode($response));
       echo json_encode($response);



    } else {
        $response["success"] = 0;
        $response["message"] = "Invalid Credentials!";
        die(json_encode($response));
    }

} else {
?>
                <h1>Login</h1>
                <form action="login.php" method="post">
                    Username:<br />
                    <input type="text" name="username" placeholder="username" />
                    <br /><br />
                    Password:<br />
                    <input type="password" name="password" placeholder="password" value="" />
                    <br /><br />
                    <input type="submit" value="Login" />
                </form>
                <a href="register.php">Register</a>
        <?php
}

?>

最佳答案

您的 JSONParser 类可能有问题,或者服务器的响应可能返回“hello”。您能否也发布该类(class)以使事情更清楚。

关于java - Bug : E/JSON Parser﹕ Error parsing data org. json.JSONException : Value Hello of type java. lang.String无法转换为JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29521736/

相关文章:

php - Laravel 5.1 如何使用迁移创建 MySQL 存储过程

java接口(interface)避免强制转换

php - MySQL 按 ID 计数值

java - 创建数据库连接的最佳方式

php - 创建数据库字段与通过编码生成数据

android - 动态改变 CardView 的背景颜色

android - 将 fragment 的上下文传递给类并显示警报对话框

Android:OAuth 签名请求

java - 这段代码的哪一部分正在减慢我的程序

java - SQL 实用函数的开源 Java 库?