我必须尝试从字符串转换为json数组。但仍然跟随错误


.addBodyParameter("checkbox1", String.valueOf(s.getCheckbox1()))


我使用该代码从字符串转换为jsonarray

这是我的完整代码,

public void add(Spacecraft s, final View...inputViews)
{
    if(s==null)
    {
        Toast.makeText(c, "No Data To Save", Toast.LENGTH_SHORT).show();
    }
    else
    {
        AndroidNetworking.post(DATA_INSERT_URL)
                .addBodyParameter("action","save")
                .addBodyParameter("checkbox1", String.valueOf(s.getCheckbox1()))
                .setTag("TAG_ADD")
                .build()
                .getAsJSONArray(new JSONArrayRequestListener() {
                    @Override
                    public void onResponse(JSONArray response) {

                        if(response != null)
                            try
                            {
                                String responseString = response.get(0).toString();
                                Toast.makeText(c, "PHP SERVER RESPONSE : " + responseString, Toast.LENGTH_SHORT).show();


                            } catch (JSONException e) {
                                e.printStackTrace();
                                Toast.makeText(c, "GOOD RESPONSE BUT JAVA CAN'T PARSE JSON IT RECEIVED : "+e.getMessage(), Toast.LENGTH_SHORT).show();
                            }

                    }

                    @Override
                    public void onError(ANError anError) {
                        Toast.makeText(c, "UNSUCCESSFUL :  ERROR IS : "+anError.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
    }
}

最佳答案

String.valueOf(s.getCheckbox1())对我来说毫无意义。也许您想使用s.getCheckbox1().isChecked()检查状态

关于android - java.lang.string无法转换为JSONarray,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52700174/

10-09 12:36