Value <br of type java.lang.String cannot be converted to JSONObject on android

后端 未结 2 806
野性不改
野性不改 2020-11-30 14:45

This is my java code and i am getting problem in this: W/System.err(1362): org.json.JSONException: Value

public class Main extends Activity {

    // label to d         


        
相关标签:
2条回答
  • 2020-11-30 15:09

    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.

    0 讨论(0)
  • 2020-11-30 15:09

    You have to edit the php configuration file.

    Find the line:

    error_reporting = E_ALL
    

    and replace with:

    error_reporting = E_ALL ^ E_DEPRECATED
    

    If you don't have access to the configuration file you can add this line to the php wordpress file (maybe headers.php):

    error_reporting(E_ALL ^ E_DEPRECATED);
    
    0 讨论(0)
提交回复
热议问题