繁体   English   中英

如何在Java中修复此错误JSON JSONObject文本必须在1 [字符2第1行]处以“ {”开头

[英]how to fix this error JSON in java A JSONObject text must begin with '{' at 1 [character 2 line 1]

我已经设法将数据保存在BD中,但是当我转到JSONobject时,它转到异常并在METHOD POST中显示此错误:

public int insertarCliente(Cliente c){
        //globalURL +=  "?rut=" + c.getRut() + "&nom="+ c.getNombre() +"&app=" + c.getApellidoP() + "&apm=" + c.getApellidoM();
        String globalURL = "http://localhost:60367/api/Cliente?rut=" + c.getRut() + "&nom="+ c.getNombre() +"&app=" + c.getApellidoP() + "&apm=" + c.getApellidoM();
        try {
            HttpURLConnection conn = Conectar(globalURL);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("ACCEPT", "application/json");
            //conn.setRequestProperty("Content-Length", "0");
            conn.setDoOutput(true);
            conn.getOutputStream().close();
            if (conn.getResponseCode() == 200) {
                InputStreamReader isr = new InputStreamReader(conn.getInputStream());
                BufferedReader br = new BufferedReader(isr);
                String resp = br.readLine();
                JSONObject obj = new JSONObject(resp.trim());
                //JSONArray obj = new JSONArray(resp.trim());
                br.close();
                return obj.getInt("resp");
            }
        } catch (Exception e) {
            Logger.getLogger(ClienteDAO.class.getName()).log(Level.SEVERE, null, e);

        }
        return 0;
    }

并显示以下错误。

Grave:   org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
    at org.json.JSONTokener.syntaxError(JSONTokener.java:432)
    at org.json.JSONObject.<init>(JSONObject.java:184)
    at org.json.JSONObject.<init>(JSONObject.java:310)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM