【问题标题】:Error: Expected BEGIN_ARRAY but was BEGIN_OBJECT错误:应为 BEGIN_ARRAY,但为 BEGIN_OBJECT
【发布时间】:2016-05-29 14:20:35
【问题描述】:

进程:com.example.venky.httpurl1,PID:2578 java.lang.RuntimeException:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为 BEGIN_ARRAY,但在第 1 行第 3 列路径 $[0]

处为 BEGIN_OBJECT
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView) findViewById(R.id.rtlist);
    client = new AsyncHttpClient();
    client.get(MainActivity.this, url, new com.loopj.android.http.AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody) {
            String responsestr = new String(responseBody);
            gson = new Gson();
            responseObj = gson.fromJson(responsestr, Response.class);
            adapter = new CustomAdapter(MainActivity.this, responseObj);
            listView.setAdapter(adapter);
        }

请输入任何内容? 是因为我的 JSON 数据吗??

【问题讨论】:

    标签: android-studio android gson


    【解决方案1】:

    在将responseBody 转换为字符串并将其解析为JSON 值之前检查是否为NULL 或Empty

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.rtlist);
        client = new AsyncHttpClient();
        client.get(MainActivity.this, url, new com.loopj.android.http.AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody) {
                if(responseBody != null && responseBody.length > 0) {
                    String responsestr = new String(responseBody);
                    gson = new Gson();
                    responseObj = gson.fromJson(responsestr, Response.class);
                    adapter = new CustomAdapter(MainActivity.this, responseObj);
                    listView.setAdapter(adapter);
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 2013-08-10
      • 2015-01-01
      • 2014-12-03
      • 2017-10-16
      相关资源
      最近更新 更多