You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Android Studio中‘null json data’错误修复及数组转JSONArray问题排查

问题分析

从你的日志list ja [null,null]和报错信息来看,核心问题出在将自定义对象列表直接转换为JSONArray的方式错误,以及后续错误地尝试从JSONArray中获取JSONObject:

  • 你的db.afficherTousProduit()返回的应该是自定义的产品对象列表(比如ArrayList<Produit>),而不是JSON格式的字符串列表。直接把这个列表传给JSONArray构造函数,它无法正确将对象序列化为JSON,最终得到的是包含null的JSONArray。
  • 之后你用ja.optJSONObject(i)尝试获取JSONObject,自然得到null,调用getString()就会触发null object reference错误。
正确的解决方案

你需要手动将每个产品对象转换为JSONObject,再添加到JSONArray中,然后将整个JSONArray作为参数传递给服务器。修改代码如下:

public void sendCommande (){
    bt_newCommande.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            StringRequest stringRequest = new StringRequest(Request.Method.POST, url_ProdCommande,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            Log.d(TAG,"element envoie !");
                            // listProRest.clear();
                            // adapter.notifyDataSetChanged();
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e(TAG, "Erreur lors de l'envoi: " + error.getMessage());
                }
            }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String,String> params = new HashMap<>();
                    // 获取产品对象列表
                    ArrayList<Produit> produitList = db.afficherTousProduit();
                    JSONArray ja = new JSONArray();
                    
                    for(Produit produit : produitList){
                        try {
                            JSONObject produitObj = new JSONObject();
                            // 将产品的各个属性放入JSONObject
                            produitObj.put("idProduit", produit.getIdProduct());
                            produitObj.put("qte", produit.getQtePro());
                            produitObj.put("heure", produit.gethCollete());
                            produitObj.put("date", produit.getDateCollete());
                            // 将JSONObject添加到JSONArray
                            ja.put(produitObj);
                        } catch (JSONException e) {
                            e.printStackTrace();
                            Log.e(TAG, "Erreur lors de la création du JSONObject: " + e.getMessage());
                        }
                    }
                    
                    Log.d(TAG,"list ja "+ja.toString());
                    // 将JSONArray转为字符串作为参数传递
                    params.put("produits", ja.toString());
                    return params;
                }
            };
            RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
            requestQueue.add(stringRequest);
        }
    });
}
关键修改点说明
  • 正确序列化对象:遍历产品对象列表,逐个将属性存入JSONObject,再添加到JSONArray,确保JSON结构正确。
  • 传递JSON字符串:将最终的JSONArray转为字符串,作为一个参数(比如produits)传递给服务器,服务器端可以解析这个字符串得到JSON数组。
  • 错误日志补充:在onErrorResponse中添加日志,方便排查请求失败的原因。

另外,需要确认你的服务器端接口是否接受produits这个参数名,并且能正确解析JSON格式的字符串。如果服务器期望的是多个独立参数(比如多个idProduitqte),那你需要调整参数传递方式,但通常传递整个JSON数组会更简洁规范。

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

火山引擎 最新活动