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

Android Studio运行报错:Expected BEGIN_ARRAY but was STRING 求助

Fixing "Expected BEGIN_ARRAY but was STRING" Error in Android Studio

Hey there, let's work through this annoying JSON parsing error you're seeing—this is super common when dealing with network responses and model classes, so let's break down exactly what's happening and how to fix it.

What's Causing This Error?

This message from your JSON parser (almost always Gson, since it's the go-to for Android) means one simple thing: your code expects the root of the JSON response to be an array ([]), but instead it received a plain string (like "Invalid request" or even an empty string) at the very first character of the response. The screenshots you shared confirm this is a parsing mismatch between what your code expects and what the server (or data source) is actually sending.

Step-by-Step Fixes

Let's walk through the most effective ways to diagnose and fix this:

  • First, see exactly what you're receiving
    You can't fix a parsing issue if you don't know the actual response content. Try one of these:

    1. Use Android Studio's Network Inspector (find it next to Logcat) to capture the request and view the raw response body.
    2. Add a log statement in your code to print the full response string before parsing:
      String rawResponse = response.body().string();
      Log.d("JSON_CHECK", "Raw server response: " + rawResponse);
      

    Chances are you'll find something unexpected—like an error message string instead of the array you were hoping for.

  • Match your model class to the actual JSON structure
    If your code is trying to parse into a List<YourModel> (array of objects) but the response is a single string, that's the problem. For example:

    • Wrong approach (expecting array, getting string):
      List<User> users = gson.fromJson(rawResponse, new TypeToken<List<User>>(){}.getType());
      
    • If the response is an error string, first check if the request succeeded (e.g., HTTP 200 status code), then handle errors separately before trying to parse the array.
  • Add error handling to avoid crashes
    Wrap your parsing code in a try-catch block to gracefully handle mismatches, and log details to debug faster:

    try {
        List<YourModel> dataList = gson.fromJson(rawResponse, new TypeToken<List<YourModel>>(){}.getType());
        // Proceed with using your parsed data
    } catch (JsonSyntaxException e) {
        Log.e("PARSE_ERROR", "Failed to parse JSON: " + e.getMessage());
        Log.e("PARSE_ERROR", "Raw response causing issue: " + rawResponse);
        // Show an error message to the user, like "Failed to load data"
    }
    
  • Double-check your request details
    Sometimes the issue isn't parsing—it's that you're calling the wrong API endpoint, or sending incorrect parameters. If the server gets a bad request, it might send back an error string instead of the expected array. Verify your request URL, headers, and body match what the API documentation requires.

Example Scenario

Suppose you expected a response like this array:

[{"id":1, "name":"Alice"}, {"id":2, "name":"Bob"}]

But instead the server returned:

"Invalid API key provided"

Trying to parse that string as an array will trigger exactly the error you're seeing. In this case, you'd first check the API key in your request, then update your code to handle error strings from the server.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:58:41