Value

About your HTML

You’re trying to get rid of some HTML code that actually tells you there is an error. Here is what it looks like:

interpreted HTML response

Maybe if you solve that problem, your JSON issue will vanish on its own, because you won’t have HTML stuff before your JSON.


Earlier part of the response

I guess you are getting your exception at this line (please tell me if I’m wrong):

jsonResponse = new JSONObject(Content);

If you want to know what’s wrong with the parsing of Content, you might want to log that string and see how it looks.

Apparently, Content is HTML code (It contains some <br, according to the exception) instead of a proper JSON String. Take a look at where you get this string from, this is probably your problem.


UPDATE:

OK, according to what you posted, your Content string contains the JSON string (the one surrounded by braces {}) but it also contains an HTML part, which needs to be removed.

Put this code before you try to create the JSONObject:

int jsonStart = Content.indexOf("{");
int jsonEnd = Content.lastIndexOf("}");

if (jsonStart >= 0 && jsonEnd >= 0 && jsonEnd > jsonStart) {
    Content = Content.substring(jsonStart, jsonEnd + 1);
} else {
    // deal with the absence of JSON content here
}

UPDATE 2:

The previous code snippet does not seem sufficient because your HTML contains braces ({}). If the server always returns that kind of HTML, you might want to just get rid of the font tag before running the previous snippet:

Content = Content.replaceFirst("<font>.*?</font>", "");

WARNING: This is very sketchy, and answers only this very particular issue. It won’t work with every possible server response.

You should have a look at other questions regarding the removal of HTML from a String in Java, for more complete answers.

Leave a Comment