JSON returning <html>

Asked

Viewed 388 times

4

I am returning a JSON from a server using PHP with simple code:

<?php
$response = array();
$response["success"] = 1;
echo json_encode($response);

I’m getting the answer through an android app, but with the following error:

Error Parsing data org.json.Jsonexception: Value <html> of type java.lang.String cannot be converted to Jsonobject

I suspect the server is sending something else along with JSON. What do you do to prevent this?

  • 2

    It would be good to test the returned content. There may have been an exception and the apache error page is being returned, for example. Can you simulate the call from a browser or in some other way?

  • 3

    Surely some exception is being returned... Kick error 500 or 404

  • That’s right, apache is sending an error page instead of JSON. Thank you!

1 answer

3


The Error received

In a general way, the error Error parsing data org.json.JSONException ... refers to the inability to process JSON.

For your particular case, it can be read in the error message:

Error Parsing data org.json.Jsonexception: Value of type java.lang.String cannot be converted to Jsonobject

That translated:

Error processing data org.json.Jsonexception: Value of type java.lang.String cannot be converted to Jsonobject

That tells us that a string instead of an object, as would be expected.

Deal with the mistake

One way to deal with the error in scenarios like this is to wrap your code in one try...catch so as to try work the data received while able to catch any errors in a way that allows you to deal with them:

try {
    JSONObject jobj = new JSONObject(response);
    String succes = jobj.getString("success");
    ...
} catch (JSONException e) {
    // ups, correu mal... vamos ver o que aconteceu
    e.printStackTrace();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.