简体   繁体   English

java.lang.String无法转换为JSONObject

[英]java.lang.String can't be converted to JSONObject

I've got a database which I use in my android app (accounts). 我有一个要在我的Android应用程序(帐户)中使用的数据库。 I want the user to be able to change his/her password. 我希望用户能够更改他/她的密码。
I'm using java, mySQL, PHP and JSON. 我正在使用java,mySQL,PHP和JSON。
The username needs to be updated by their id, I just created the code to do this but I get an error...: "Error parsing data org.json.JSONException: Value < br>< table of type java.lang.String cannot be converted to JSONObject". 用户名需要通过其ID更新,我只是创建了代码以执行此操作,但出现错误...:“解析数据org.json.JSONException时出错:值<br> <类型为java.lang.String的表无法转换为JSONObject”。

Here are some lines of code, I use: 这是我使用的一些代码行:

PHP 的PHP

$query = "UPDATE users SET username = :username WHERE id = :id";
$stmt   = $db->prepare($query);
$result = $stmt->execute($query_params);

I also need to check if the username is succesfully updated, but I'll do that later. 我还需要检查用户名是否已成功更新,但稍后再做。
I just use: 我只用:

$response["success"] = 1;
$response["message"] = "Username is succesfully changed!";
die(json_encode($response));

JAVA 爪哇

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
id = sp.getInt("id", 1);
EditText newUsername = (EditText)findViewById(R.id.etNewUsername);
username = newUsername.getText().toString();

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", Integer.toString(id)));
params.add(new BasicNameValuePair("username", username));
Log.d("request!", "starting");
JSONObject json = jsonParser.makeHttpRequest(url, "POST", params);
Log.d("After updating username", json.toString());

But the Tag: "After updating username" doesn't appear after the error in the LogCat. 但是在LogCat中出现错误后,不会出现标签:“更新用户名之后”。
So I know the error is within the HttpRequest. 所以我知道错误是在HttpRequest中。
Maybe it's the id? 也许是身份证? Because it will be converted to a String and in the database the id is an integer? 因为它将被转换为字符串,并且在数据库中,id是整数?

Every help will be appreciated! 每一个帮助将不胜感激!
Thanks. 谢谢。

Error parsing data org.json.JSONException: Value < br>< table of type java.lang.String cannot be converted to JSONObject

Notice the value- "<br> <table" . 注意值"<br> <table" You're trying to treat HTML as JSON. 您正在尝试将HTML视为JSON。 That doesn't work, so the JSONParser is throwing an exception. 那是行不通的,因此JSONParser抛出异常。 You don't catch the exception, so you crash. 您没有捕获到异常,因此崩溃了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM