我正在尝试使用javaapi显示来自gmail的电子邮件。
当用户单击消息行时,邮件将打开并显示其消息正文。为此,我编写了一个javascript函数:

function viewMail() {

    $('#table tbody').unbind().on(
            'click',
            'tr td:not(.email-select)',
            function() {

                var messageNumber = $(this).parent().children('td:eq(0)')
                        .children().val();
                var from = $(this).parent().children('td:eq(1)').text();
                var subject = $(this).parent().children('td:eq(2)').text();
                var dateAndTime = $(this).parent().children('td:eq(3)').text();
                var seen = $(this).parent().children('td:eq(4)').text();
                var folderName = $(this).parent().children('td:eq(5)').text();
                /*
                 * $.post("/Webclient/getMail", {messageId:messageId},
                 * function(data){
                 */
                var data = {"messageNumber":messageNumber,
                        "seen":seen,
                        "folderName":folderName
                        };

                $.ajax({
                    type : "GET",
                    url : "/Webclient/showMail",
                    contentType : "application/json; charset=utf-8",
                    data : JSON.stringify(data),
                    success : function(result) {
                        $('#email_subject').text(subject);
                        $('#sender_name').text(from);
                        $('#date_and_time').text(dateAndTime);
                        $('#messageBody').empty();
                        $('#messageBody').append(result.content);
                        $('#emailModal').modal('show');
                    }

                });

            });

}

而这在我的 Controller 方法中:
@RequestMapping(value = "/showMail", method = RequestMethod.GET)
    public @ResponseBody
    String showMail(HttpServletRequest request) throws IOException{

        JSONObject result = new JSONObject();
        StringBuilder buffer = new StringBuilder();
        BufferedReader reader = request.getReader();
        String line;
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
        String emailInfo = buffer.toString();

        //This is the line I am getting error
        JSONObject jsonObject = new JSONObject(emailInfo);
        System.out.println(emailInfo);

        User authUser = (User) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();
        String userName = authUser.getUsername();
        String password = SecurityContextHolder.getContext()
                .getAuthentication().getCredentials().toString();

        String message = null;

             try {
                message = imapService.showMail(userName,password,jsonObject);
                result.append("message",message);
            } catch (MessagingException e) {
                result.append("message","Could not open the message");
                e.printStackTrace();
            }

             return result.toString();

    }

错误:
org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
    org.json.JSONTokener.syntaxError(JSONTokener.java:433)
    org.json.JSONObject.<init>(JSONObject.java:194)
    org.json.JSONObject.<init>(JSONObject.java:321)
    controller.ImapController.showMail(ImapController.java:173)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)

最佳答案

错误消息中的行“JSONObject文本必须以'{'开头”使我怀疑问题出在emailinfo构造函数中的JSONObject

我不能说出emailinfo的内容是从这里来的,但是我敢打赌,如果您查看打印出的emailinfo的内容,您会发现emailinfo不是正确的JSON格式。

正确的JSON格式示例如下:

{"key": {"second_key": "value"}}

另外,我建议以下站点检查emailinfo是否为有效的JSON:http://jsonlint.com

09-25 17:35