【问题标题】:informacioncom.android.volley.ParseError: org.json.JSONException: End of input at character 0 ofinformationacioncom.android.volley.ParseError: org.json.JSONException: End of input at character 0 of
【发布时间】:2018-10-14 07:43:41
【问题描述】:

我在尝试使用 PUT 方法时遇到问题,我正在使用 PUT 方法通过 Web 服务更新我的表记录,问题是在尝试更新数据时出现以下错误:

informacioncom.android.volley.ParseError: org.json.JSONException: End 在字符 0 处输入的

如果我检查数据库中的表,它不会输入空值,它会记录空格。

这是我用来更新数据的方法:

@Override
    public void onClickUpdate(final int position, final String identificador) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = this.getLayoutInflater();
        View MyView = inflater.inflate(R.layout.dialog_signin_updatedonante, null);

        final EditText identidad = (EditText)MyView.findViewById(R.id.identificador_tarea);
        final EditText nombre_tarea = (EditText)MyView.findViewById(R.id.nombre_tarea);
        final EditText nota = (EditText)MyView.findViewById(R.id.nota_tarea);
        final EditText estudiante = (EditText)MyView.findViewById(R.id.estudiante_tarea);
        identidad.setEnabled(false);
        identidad.setText(identificador);

        String ntarea = nombre_tarea.getText().toString();
        String not = nota.getText().toString();
        String estudi = estudiante.getText().toString();

        final JSONObject jsonObject = new JSONObject();
        try{
            jsonObject.put("nombreUsuario", ntarea)
                    .put("nota", not)
                    .put("estudiante", estudi);
        }catch(JSONException e){
            e.printStackTrace();
        }

        final RequestQueue queue = Volley.newRequestQueue(getActivity());

        builder.setView(MyView)
                .setPositiveButton("Registrar", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        if(nombre_tarea.getText().toString().equals("") || nota.getText().toString().equals("") || estudiante.getText().toString().equals("")){
                            Toast.makeText(getActivity(), "Porfavor llena todos los campos", Toast.LENGTH_LONG ).show();
                        }else{
                            String urlPut = "http://192.168.1.81:8080/WebServiceExamenFinal/webapi/tareas/";

                            JsonObjectRequest request = new JsonObjectRequest(Request.Method.PUT, urlPut + identificador, jsonObject,
                                    new Response.Listener<JSONObject>() {
                                        @Override
                                        public void onResponse(JSONObject response) {

                                            Toast.makeText(getActivity(), "Se envió correctamente", Toast.LENGTH_LONG).show();
                                        }
                                    }, new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    Toast.makeText(getActivity(), "Ocurrió un error al enviar la información"+error, Toast.LENGTH_LONG).show();
                                }
                            });
                            queue.add(request);
                        }
                    }
                })
                .setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Toast.makeText(getActivity(),"No se actualizo ningun registro", Toast.LENGTH_SHORT).show();
                        alertDialog.dismiss();
                    }
                });
        alertDialog = builder.create();
        alertDialog.show();

    }

【问题讨论】:

    标签: android web-services android-volley


    【解决方案1】:

    看起来您的 jsonObject 变量在您向其中添加数据之前已声明为最终变量。最好的解决方案是将您的代码拆分为单独的函数,但现在您可以尝试以下方法:

    JSONObject tempJson = new JSONObject();
    try{
    tempJson.put("nombreUsuario", ntarea)
                .put("nota", not)
                .put("estudiante", estudi);
    } catch(JSONException e){
        e.printStackTrace();
    }
    final JSONObject jsonObject = tempJson;
    

    【讨论】:

    • 我已经尝试过了,但错误仍然存​​在,我没有更改任何内容
    • 查看另一个 SO 问题,看起来您得到的回复可能是空的?可能是当您运行 PUT 请求时,返回的响应没有正文。我会尝试使用调试器或日志语句运行代码,看看你能找到哪些额外的信息。 json 对象在传递给 volley 请求之前是什么样子的?参考问题:stackoverflow.com/questions/32104423/…
    猜你喜欢
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    相关资源
    最近更新 更多