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

使用JsonObjectRequest调用API获取JSON时触发JSONException错误求助

解决Volley JsonObjectRequest POST请求的JSONException问题

你遇到的org.json.JSONException: End of input at character 0 of错误,本质是Volley收到了空的响应体,导致无法解析JSON。结合你的代码和API返回示例,我整理了几个关键修复点:

1. 修正POST参数的传递方式

你当前把POST请求的参数直接拼在了URL上,但POST请求的参数通常应该放在请求体(Request Body)中,而非URL的Query部分。很多服务器会忽略POST请求URL里的参数,导致返回空响应,触发JSON解析失败。

修复方案:构建请求体JSON

把参数封装成JSONObject,作为JsonObjectRequest的第三个参数传入:

private void execute(final Context context) {
    String url = UrlManager.getUrl(context, R.string.signup_url);
    User user = User.getInstance();

    // 构建POST请求体的参数
    JSONObject params = new JSONObject();
    try {
        params.put("name", mFirstName);
        params.put("last", mLastName);
        params.put("mob", mMobile);
        params.put("email", mEmail);
        params.put("pass", UrlManager.prepareUrlPart(mPassword));
    } catch (JSONException e) {
        e.printStackTrace();
    }

    final ProgressDialog pDialog;
    pDialog = new ProgressDialog(context);
    pDialog.setMessage("Loading...");
    pDialog.setCancelable(false);
    pDialog.show();

    Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                userInfo = new UserInfo();
                userInfo.LoadItem(context, response);
                String state = response.getString("State");
                
                // 注意:String比较要用equals,不是==
                if ("1".equals(state)) {
                    onResult(true);
                } else {
                    onResult(false);
                }
            } catch (Exception e) {
                Log.d("Error", e.getMessage());
                e.printStackTrace();
            }
            pDialog.dismiss();
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
            pDialog.dismiss();
        }
    };

    // 传入params作为请求体
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, params, listener, errorListener);
    
    // 可选:添加请求头,确保服务器识别JSON格式
    request.setHeaders(new HashMap<String, String>() {{
        put("Content-Type", "application/json; charset=utf-8");
    }});
    
    // 可选:禁用缓存,避免旧的空响应干扰
    request.setShouldCache(false);
    
    MyApplication.getInstance().addToRequestQueue(request);
}

2. 修复字符串比较的错误

你的代码里用state == "1"判断状态,这在Java中是错误的——==比较的是对象引用,而非字符串内容。应该改为"1".equals(state)(这种写法还能避免空指针异常)。

3. 额外排查点

如果上述修复后仍有问题,可以检查:

  • 确认服务器支持接收JSON格式的POST请求体
  • 检查UrlManager.prepareUrlPart是否正确处理了特殊字符(如空格、中文等),避免参数编码错误导致服务器无法处理请求
  • 用Postman直接调用API,确认传入正确参数时能返回预期的JSON响应

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 07:03:03