Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObjectShort 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.
Fix it fast
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.php2Match what you see, then jump to its fix
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.log3Verify
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 120Confirmed by php.net
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.php2Fix
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 APIConfirmed by developer.android.com
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:
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- Nuclear Home VideoA video-streaming Android app built from scratch, engineered for smooth playback.
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
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 fixBack end
A Gradle build fails on classes that use Lombok annotations
Error: Annotation processors must be explicitly declared now.
Lombok is two things for Gradle: a compile-time library and an annotation processor.
3 causes, each with a fixBack end
Gradle 8 fails on Report destination: switch to outputLocation
What went wrong: 'java.io.File org.gradle.api.reporting.ConfigurableReport.getDestination()'
Gradle 8.0 removed the deprecated destination and enabled properties of the Report type.
2 causes, each with a fixBack end
IntelliJ IDEA compiles with a JDK that does not know Java 17
Invalid source release: 17
The compiler that IntelliJ or Gradle starts is older than Java 17, but the project asks for Java 17 source.
3 causes, each with a fixBack end
A scanner flags jackson-databind: how to upgrade it in Gradle
jackson-databind vulnerability reported by a dependency scanner
Find where the old jackson-databind comes from, then raise it.
3 causes, each with a fixBack end
Gradle cannot resolve spring-boot-gradle-plugin after a Spring Boot 3 upgrade
Plugin [id: 'org.springframework.boot', version: '3.0.1'] was not found in any of the following sources
Spring Boot 3 needs Java 17 or newer and a recent Gradle, and its Gradle plugin is published on the Gradle Plugin Portal.
3 causes, each with a fix