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

Android解析JSON遇org.json.JSONException: No value for status错误求助

解决Volley JSON解析的JSONException: No value for status及参数传递问题

让我一步步帮你排查并解决问题:

1. 直接触发JSONException的代码错误

看你的Android代码里这段明显的逻辑问题:

JSONObject array = new JSONObject(); // 新建了一个完全空的JSON对象
Log.i("test", " value : " + response.getString("status")); // 这里明明能从服务器返回的response拿到status
if (array.getString("status").equals("true")) { // 却去空对象里取status,肯定报错!

你已经成功获取到了服务器返回的response对象,甚至已经打印出了status值为4,但你却额外创建了一个空的JSONObject,然后试图从这个空对象里读取status字段——这必然会抛出No value for status的异常!

修复方法:直接使用服务器返回的response对象操作,删掉空的JSONObject创建代码,同时注意PHP返回的status是数字类型,不是字符串"true":

// 删掉这句无效代码:JSONObject array = new JSONObject();
Log.i("test", " value : " + response.getString("status"));
// 用getInt获取数字类型的status
int status = response.getInt("status");
if (status == 1) { 
    // PHP返回的书籍列表键是"books",不是你写的"search"
    JSONArray jsonArray = response.getJSONArray("books"); 
    // 后续遍历逻辑...
}

2. 为什么PHP返回status=4(参数未正确传递)

你的Android代码用了JsonObjectRequest发送POST请求,但JsonObjectRequest默认会把请求体以JSON格式发送,而你的PHP代码是用$_POST["Title"]来接收表单格式的参数,这就导致PHP完全收不到参数,进入else分支返回status=4。

这里有两种解决方案:

方案一:改用StringRequest发送表单参数(推荐)

StringRequest会自动把getParams()里的参数以表单格式发送,和PHP的$_POST接收逻辑匹配:

public void performSearch() {
    String url= "http://192.168.0.136/fyp/stitle1.php";
    RequestQueue requestQueue = Volley.newRequestQueue(Stitle.this);
    
    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.d("Response", response);
                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        int status = jsonResponse.getInt("status");
                        Log.i("test", " value : " + status);
                        if (status == 1) {
                            JSONArray jsonArray = jsonResponse.getJSONArray("books");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject product = jsonArray.getJSONObject(i);
                                boolean add = productList.add(new list(
                                        product.getLong("isbn"),
                                        product.getString("title"),
                                        product.getString("authors"),
                                        product.getInt("accession"),
                                        product.getString("publisher"),
                                        product.getInt("pubyear"),
                                        product.getInt("pages"),
                                        product.getInt("rak"),
                                        product.getInt("hr"),
                                        product.getInt("vr"),
                                        product.getLong("barcode")
                                ));
                            }
                        } else {
                            Log.i("test", "else error: " + jsonResponse.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.i("test", e.toString());
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(), "error:" + error.toString(), Toast.LENGTH_LONG).show();
                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("Title", searchtitle.getText().toString());
            return params;
        }
    };
    
    requestQueue.add(stringRequest);
}

方案二:修改PHP代码接收JSON请求体

如果坚持要用JsonObjectRequest,需要在Android端把参数封装成JSONObject作为请求体,同时PHP端读取JSON格式的请求内容:

  • Android端修改请求体:
JSONObject requestBody = new JSONObject();
try {
    requestBody.put("Title", searchtitle.getText().toString());
} catch (JSONException e) {
    e.printStackTrace();
}

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, url, requestBody,
        // 原有Listener逻辑...
);
  • PHP端修改参数接收逻辑:
// 读取JSON格式的请求体
$input = file_get_contents('php://input');
$data = json_decode($input, true);

if (isset($data["Title"])) {
    $title = $data["Title"];
    // 后续数据库查询逻辑...
} else {
    $response["status"] = 4;
    $response["message"] = "Post parameters are not correct";
}

额外小提示

你的PHP代码里返回的书籍列表键是"books",但原Android代码里写的是array.getJSONArray("search"),这个也要同步改成response.getJSONArray("books"),不然会触发新的JSON异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:08:39