简体   繁体   English

将BufferedReader解析为JSON对象时,位置处出现意外的令牌END OF FILE

[英]Unexpected token END OF FILE at position while parsing BufferedReader to JSON Object

I am passing an object to the Java Servlet publishStory using Angularjs $http service. 我正在使用Angularjs $http服务将对象传递给Java Servlet publishStory And however this same servlet code has worked fine with other requests but here I am getting the following error: 但是,这个相同的servlet代码可以与其他请求一起正常工作,但是在这里,我得到了以下错误:

cmd错误日志

I have researched it on the internet but could not find solution to this particular case where I am using BufferedReader, as the same servlet works fine with other requests. 我已经在Internet上对其进行了研究,但是在使用BufferedReader的这种特殊情况下找不到解决方案,因为同一servlet可以与其他请求一起正常工作。

Here is my publishStory servlet: 这是我的publishStory servlet:

public class publishStory extends HttpServlet{
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
    PreparedStatement pstmt = null;
    Connection cn = null;
    ResultSet rs = null;
    boolean flag = false;
    PrintWriter out = null;
    try{
        System.out.println("CALLING THE METHOD");
        Class.forName("com.mysql.jdbc.Driver");
        cn = DriverManager.getConnection("jdbc:mysql://localhost/storyboard","root","");

        BufferedReader reader = request.getReader();
        JSONParser parser = new JSONParser();
        JSONObject juser = null;

        juser = (JSONObject)parser.parse(reader);

        String welcomeStoryTitle = (String)juser.get("welcomeStoryTitle");
        String welcomeStoryWords = (String)juser.get("welcomeStoryWords");

        String query = "INSERT INTO stories (storytitle, storytext) VALUES (?,?)";
        pstmt = cn.prepareStatement(query);
        pstmt.setString(1, welcomeStoryTitle);
        pstmt.setString(2, welcomeStoryWords);
        int i = pstmt.executeUpdate();
        if(i>0)
            System.out.println(welcomeStoryTitle=" : "+welcomeStoryWords);
        else
            System.out.println("No data inserted");
    }
    catch(Exception e){
        e.printStackTrace();
    }
}

I am retrieving two request parameters welcomeStoryTitle and welcomeStoryWords in this servlet. 我正在此servlet中检索两个请求参数welcomeStoryTitlewelcomeStoryWords

And below is my angularjs controller with a function implementing $http service: 下面是我的angularjs控制器 ,带有实现$http服务的函数:

app.controller('welcomeStoryPublishCtrl', ['$log','$http',function($log,$http){
    this.publishGroup = function(wsPublish){
        $http({
            method:'POST',
            url: 'http://localhost:9090/Writer/publishStory',
            header: {'Content-Type':'application/json'},
            data: wsPublish
        }).success(function(){});
    };
}]);

I am getting this error where I am parsing the reader object, at line: 我在以下行中解析阅读器对象时遇到此错误:

juser = (JSONObject)parser.parse(reader);

Could this be because of the size of a String being retrieved from request? 这可能是因为从请求中检索到的字符串的大小吗?

'At position 0' is a giveaway. “在位置0”是赠品。 You are trying to parse an empty response. 您正在尝试解析一个空的响应。

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

相关问题 解析 JSON 时位置 0 处出现意外标记 END OF FILE - Unexpected token END OF FILE at position 0 while parsing JSON 位置0(JSON)处出现意外令牌'文件结束' - Unexpected token 'END OF FILE' at position 0 (JSON) 在Java中解析时,JSON值中的空格会导致“位置11处的意外令牌END OF FILE”异常 - A space in JSON value causes “unexpected token END OF FILE at position 11” exception when parsing in Java Java Eclipse - 尝试从 JSON 读取时“位置 0 处出现意外令牌 END OF FILE” - Java Eclipse - 'Unexpected token END OF FILE at position 0' when trying to read from JSON System.Xml.XmlException:解析时文件意外结束 - System.Xml.XmlException: Unexpected end of file while parsing 解析 BufferedReader object 时出现问题 - Issue in parsing a BufferedReader object 使用 BufferedReader 解析文件 - parsing a file with BufferedReader 错误:语法错误:JSON 中的意外标记 g 在位置 0 - error:SyntaxError: Unexpected token g in JSON at position 0 Fetch API in React for JSON file: Uncaught (in promise) SyntaxError: Unexpected token � in JSON at position 0 - Fetch API in React for JSON file: Uncaught (in promise) SyntaxError: Unexpected token � in JSON at position 0 解析时到达文件末尾 - reached end of the file while parsing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM