0

I've got a database which I use in my android app (accounts). I want the user to be able to change his/her password.
I'm using java, mySQL, PHP and 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".

Here are some lines of code, I use:

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.
So I know the error is within the HttpRequest.
Maybe it's the id? Because it will be converted to a String and in the database the id is an integer?

Every help will be appreciated!
Thanks.

4
  • Are you using "JSONParser" code copied from here: techlovejump.com/android-json-parser-from-url ??? If so, beware that the code contains a variety of bugs, and one of them is that it will fail if the server sends you an error response (or any response) with an HTML body. Commented Jan 2, 2015 at 23:39
  • Yes I use that JSONParser, but I don't use HTML... Commented Jan 3, 2015 at 0:31
  • @Joen - "I don't use HTML". That isn't the point. The server you are talking to >>is sending you HTML<< ... and that JSONParser code you have copied is too lame to cope with it. (It doesn't check the response code. It doesn't check the response content type. It has other flaws. Look for something better to copy. Or "man up" and write your own version. (Perhaps I should say "coder up" :-). ) ) Commented Jan 3, 2015 at 0:36
  • @Stephen C - Do you know how to check if the username is changed? Commented Jan 4, 2015 at 20:05

1 Answer 1

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

Notice the value- "<br> <table". You're trying to treat HTML as JSON. That doesn't work, so the JSONParser is throwing an exception. You don't catch the exception, so you crash.

Sign up to request clarification or add additional context in comments.

2 Comments

You are somewhere. Its parsing an html value. It might be an error page like a 404.
I forgot a )... Now I can change the password, do you know how I can check if the password is changed? Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.