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

Android Studio中Volley POST本地WebAPI出现解析错误求助

Volley POST请求解析错误排查

我来帮你定位这个问题的根源——你遇到的错误com.android.volley.ParseError: org.json.JSONException: Value 0 of type java.lang.Integer cannot be converted to JSONObject已经说得很清楚了:你的WebAPI返回的是纯整数0,但你使用的JsonObjectRequest要求响应必须是合法的JSON对象,Volley在尝试把返回的0转换成JSONObject时自然就失败了。

为什么Postman能正常工作?

Postman是通用的API测试工具,它不会强制校验响应的格式类型,不管返回的是纯数字、字符串还是JSON对象,它都能直接展示。但Volley的JsonObjectRequest是专门设计用来接收JSON对象响应的,对格式的要求非常严格。

解决方案

根据你的实际需求,有两种处理方式:


方式一:如果API确实只返回纯数字(比如状态码),改用StringRequest

把JsonObjectRequest替换成StringRequest,它可以接收任意格式的字符串响应,之后你再自行处理返回的内容:

RegisterButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        RequestQueue requestQueue = Volley.newRequestQueue(Registration2.this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.ROOT_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.e("Result of POST :", response);
                    // 按需转换响应内容,比如转成整数
                    int resultCode = Integer.parseInt(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("ERRORAS 2 :", error.toString());
                }
            }) {
            // 如果后端接收表单格式参数,重写getParams传递参数
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("FirstName", "Vedaanth");
                params.put("LastName", "Pradhan");
                params.put("PhoneNumber", "9*******1");
                params.put("EmailID", "p******@gmail.com");
                return params;
            }

            // 如果后端接收JSON格式请求体,重写getBody和getBodyContentType
            /*
            @Override
            public byte[] getBody() throws AuthFailureError {
                try {
                    JSONObject parameters = new JSONObject();
                    parameters.put("FirstName", "Vedaanth");
                    parameters.put("LastName", "Pradhan");
                    parameters.put("PhoneNumber", "9*******1");
                    parameters.put("EmailID", "p******@gmail.com");
                    return parameters.toString().getBytes("utf-8");
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }

            @Override
            public String getBodyContentType() {
                return "application/json; charset=utf-8";
            }
            */
        };
        requestQueue.add(stringRequest);
    }
});

方式二:修改后端API,返回合法的JSON对象

如果你的API应该返回JSON格式的数据,那需要调整后端代码,让它返回类似这样的JSON对象,而不是纯数字:

{"status": 0, "message": "注册成功"}

这样JsonObjectRequest就能正常解析响应了,你原来的代码也不需要做大的改动。

总结

核心问题就是响应格式与请求类的期望不匹配,要么换用更通用的StringRequest来接收响应,要么调整后端返回符合要求的JSON对象。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:18:57