You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java报错org.json.JSONException:无法将String类型HTML转为JSONObject

Hey there, let's break down this JSONException you're encountering and figure out how to fix it.

What's causing this error?

The exception org.json.JSONException: value html of type java.lang.string cannot be converted to jsonobject is crystal clear: you're trying to parse an HTML string as if it were a JSON object, which won't work—HTML isn't valid JSON syntax.

This means when your postTransaksi() method calls the API, the response you're getting back isn't the JSON data you expected. Instead, it's an HTML page (most likely an error page like a 404, 500, or invalid request page from the server).

Step-by-step troubleshooting

Let's walk through the most common fixes and checks:

  • First, see exactly what the API is returning
    Add logging to capture the full response content before you try to parse it. For example, if you're using OkHttp, add this right after receiving the response:

    String responseContent = response.body().string();
    Log.d("API_DEBUG", "Raw response: " + responseContent);
    

    Note: If you use string() on the response body, you can only call it once—so store it in a variable first. This will show you if the response is indeed HTML (like a "Page Not Found" or server error page).

  • Verify your request parameters are correct
    Looking at your code snippet:

    • idMeridian = tvStatus.getText().toString(): Is this field supposed to hold an ID value? Double-check that the text in tvStatus is a valid ID (not a label or status message).
    • idProject = spProject.getSelectedItem().toString(): Spinners often return the display text instead of the underlying ID value. If your API expects a numeric ID, you might be passing a human-readable name here. Use spProject.getSelectedItemId() or check how you populated the spinner to get the correct ID.
  • Check request configuration
    Make sure you're using the right HTTP method (POST, as your method name suggests), correct API endpoint URL, and required headers (like Content-Type: application/json if the API expects JSON payloads). A misconfigured request can easily trigger a server error that returns HTML instead of JSON.

  • Test the API directly
    Use tools like Postman or curl to send the same request with the same parameters your app is using. If the tool also returns HTML, the issue is on the backend (server is down, endpoint is wrong, or parameters are invalid). If it returns valid JSON, then the problem is in your app's request setup.

  • Add error handling for non-success responses
    Don't assume every API call will return valid JSON. Check the response status code first before parsing:

    if (response.isSuccessful()) {
        // Parse JSON here
    } else {
        // Handle error responses (read the error body to see HTML content)
        String errorContent = response.errorBody().string();
        Log.e("API_ERROR", "Status code: " + response.code() + ", Content: " + errorContent);
    }
    

内容的提问来源于stack exchange,提问作者Belsazer Razer B

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:31:19