Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Android gets HTML instead of JSON from a PHP API

Errororg.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

Short answer

The response body is not JSON. It starts with <br, which is the HTML PHP prints for a warning, notice or error, or an HTML error page. Print the raw response first. Then turn off display_errors and html_errors on the server, log errors to a file, and fix the PHP error itself.

  • Applies to PHP 8.3, Android 14, Android 15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Request the same URL with curl and read the first lines of the body. Whatever comes before the first brace is what breaks the parser.

    curl -i https://example.com/api/list.php
  2. 2Match what you see, then jump to its fix

    1. 1The body starts with <br /> and a message such as Warning or Fatal error, and the JSON follows it or is missing.PHP prints a warning or error into the responseFix
    2. 2HTTP status is 404 or 500 and the body is an HTML page.The server answers with an HTML error pageFix

What does the error look like?

  • org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

Cause 1 of 2

PHP prints a warning or error into the response

1Check

With display_errors on, PHP prints errors to the output, and html_errors adds HTML tags around them. That text is in front of your JSON.

php -i | grep -E 'display_errors|html_errors|log_errors|error_log'

2Fix

Log errors instead of showing them, which is what the production php.ini template does, then fix the PHP error shown in the log.

; php.ini
display_errors = Off
log_errors = On
error_log = /var/log/php/php-errors.log

3Verify

Restart PHP, call the endpoint again and check that the body begins with the JSON.

curl -s https://example.com/api/list.php | head -c 120

Confirmed by php.net

Did this fix it?

Cause 2 of 2

The server answers with an HTML error page

1Check

With HttpURLConnection, a 4xx or 5xx status makes getInputStream throw an IOException, and the server's body is available from getErrorStream. Some clients hand that HTML to the JSON parser.

curl -s -o /dev/null -w '%{http_code}
' https://example.com/api/list.php

2Fix

Check the status code before parsing, and read getErrorStream when it is not 2xx. Then fix the URL or the server error.

val code = conn.responseCode
val stream = if (code in 200..299) conn.inputStream else conn.errorStream
val body = stream.bufferedReader().use { it.readText() }
if (code !in 200..299) Log.e("API", "HTTP $code: $body")

3Verify

A wrong URL now logs the status code and body, and no JSONException appears.

adb logcat -s API

Confirmed by developer.android.com

Did this fix it?

How do I stop it coming back?

  • Treat JSON parse failures as expected: log the raw body before parsing.
  • Never leave display_errors on in a server that real apps call.
  • Return an explicit JSON error body from the API for failures.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Gradle runs no Cucumber scenarios from my feature files

Feature files exist under src/test but nothing runs

Cucumber runs on the JUnit Platform, so Gradle must call useJUnitPlatform() and have the cucumber-junit-platform-engine on the test classpath.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.