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

Android Volley获取JSON响应不完整问题求助

问题原因及解决方法

一、代码核心错误导致响应异常

你在getParams()方法里新建了空的HashMap并返回,完全没有把之前构建好的业务参数传递给服务器。这会导致请求缺少必要参数,服务器返回的响应自然和Postman中携带完整参数的请求不一致,甚至可能返回不完整的错误响应。

二、Logcat显示截断的原因

即使修复请求参数后,直接在Logcat打印完整JSON响应也可能出现显示截断——因为Logcat默认对单条日志有字符长度限制(通常4000-8000字符),长内容会被自动截断,但实际response变量中的数据是完整的,只是日志输出不全。


解决步骤

1. 修复请求参数传递问题

把前面构建的参数集合正确返回给Volley:

public void makeAssignment(final String token, HashMap<String,String> info_doc , List<Region> result, final OnMakeAssignment onMakeAssignment) {
    // 声明为final,确保内部类可访问
    final HashMap<String, String> params = new HashMap<String, String>();
    params.put("document_type", info_doc.get("type_doc"));
    params.put("title", info_doc.get("title_doc"));
    params.put("meter", info_doc.get("meter_doc"));
    params.put("price", info_doc.get("price_frosh"));
    params.put("mortgage_price", info_doc.get("vagozari_price_rahn")); // رهن
    params.put("monthly_rent_price", info_doc.get("vagozari_price_ejareh")); // ejareh
    params.put("address", info_doc.get("address"));
    params.put("description", info_doc.get("vagozari_description"));
    params.put("region[0]", "1");

    if (result.isEmpty()){
        params.put("region[0]", "all");
    }else{
        for (int i = 0; i < result.size(); i++) {
            params.put("region["+i+"]", String.valueOf(result.get(i).getId()));
        }
    }

    StringRequest stringRequest = new StringRequest(Request.Method.POST, Vars.MAKE_ASSIGNMENT,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    onMakeAssignment.onReceived(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    if (error instanceof NoConnectionError || error instanceof TimeoutError) {
                        context.startActivity(new Intent(context, NoInternet.class));
                        onMakeAssignment.onError(error.getMessage());
                    }
                }
            }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put(Vars.KEY_AUTH, token);
            headers.put(Vars.KEY_USERNAME, Vars.USERNAME);
            headers.put(Vars.KEY_PASSWORD, Vars.PASSWORD);
            return headers;
        }

        @Nullable
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            // 返回之前构建的参数集合,而不是空HashMap
            return params;
        }
    };

    // 可选:调整超时时间,避免大响应超时
    stringRequest.setRetryPolicy(new DefaultRetryPolicy(
            15000, // 15秒超时
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

    RequestQueue requestQueue = Volley.newRequestQueue(context);
    requestQueue.add(stringRequest);
}

2. 解决Logcat显示截断问题

  • 分段打印响应:把长响应拆分成多个小段输出:
    @Override
    public void onResponse(String response) {
        final int chunkSize = 4000;
        for (int i = 0; i < response.length(); i += chunkSize) {
            int end = Math.min(response.length(), i + chunkSize);
            Log.d("FullResponse", response.substring(i, end));
        }
        onMakeAssignment.onReceived(response);
    }
    
  • 写入文件查看:将响应内容写入手机存储文件,再导出查看完整内容;
  • 解析后打印关键字段:用Gson等库把JSON解析成实体类,只打印需要的字段,避免输出完整长字符串。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 17:05:28