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

安卓日志报错W/System.err: .MainActivity$1.onResponse,JSON解析失败求助

Troubleshooting JSON Parsing Error in Android MainActivity$1.onResponse

Hey there, let’s walk through how to diagnose and fix this JSON parsing issue you’re facing. The error log pointing to W/System.err: .MainActivity$1.onResponse tells us the problem occurs when your app tries to process the response from the API http://159.65.89.76/userApi/categories. Here’s a step-by-step breakdown of what to check:

  • First, verify the API returns valid JSON
    Before blaming your app code, confirm the API itself is sending properly formatted JSON. Use a tool like curl (run curl http://159.65.89.76/userApi/categories in your terminal) or Postman to fetch the response. Then paste the result into a JSON validator to check for syntax errors. It’s common for APIs to return HTML error pages (e.g., 404 Not Found, 500 Server Error) instead of JSON when something goes wrong—this will definitely break parsing.

  • Check your JSON parsing logic in onResponse
    Mismatches between your app’s data model and the API’s JSON structure are the most frequent cause of errors. For example:

    • If the API returns an array ([{"id":1, "name":"Tech"}, ...]) but you’re trying to parse it as a single JSONObject, you’ll get an exception.
    • If your model class uses categoryName but the JSON uses category_name, libraries like Gson/Moshi will fail to map the fields unless you add annotations (like @SerializedName("category_name")).
    • Double-check that you’re using the correct parser for the response type—don’t mix up JSONArray and JSONObject calls.
  • Fetch the full error stack trace
    The log snippet you shared only shows the starting point of the error. In Logcat, expand the W/System.err entry to see the full exception details. Common messages you might see include:

    JSONException: Value of type java.lang.String cannot be converted to JSONObject
    IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
    These specific errors will immediately tell you whether the issue is invalid JSON, a structure mismatch, or something else.

  • Validate the API response code
    In your onResponse method, add a log statement to print response.code(). If you get a 4xx (client error) or 5xx (server error) code instead of 200 OK, the API isn’t returning the expected data. For example, a 404 means the API endpoint might be incorrect, and a 500 means there’s a server-side issue you’ll need to address first.

  • Ensure proper handling of the response body
    Remember that response.body().string() can only be called once—if you log it and then try to parse it, you’ll get an empty string. Either parse it directly, or store the string in a variable first:

    String responseBody = response.body().string();
    Log.d("API_RESPONSE", responseBody);
    // Now parse responseBody with your JSON parser
    

内容的提问来源于stack exchange,提问作者Basuki Thakur

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 09:07:50