错误:E/JSON解析程序﹕分析数据org.json.JSONException时出错:java.lang.String


Bug: E/JSON Parser﹕ Error parsing data org.json.JSONException: Value Hello of type java.lang.String cannot be converted to JSONObject

当我点击登录按钮时,我得到了这个消息,我不知道为什么,我也不知道为什么它在错误中显示Hello,我搜索了我的整个项目,但找不到"Hello"的出现。这个错误还有其他原因吗?

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"。你能把那节课也发出来让事情更清楚吗。