将BufferedReader解析为JSON对象时,位置上的意外标记END OF FILE

时间:2016-02-28 08:57:36

标签: java angularjs json servlets

我使用Angularjs publishStory服务将对象传递给Java Servlet $http。然而,这个相同的servlet代码与其他请求一起正常工作但在这里我收到以下错误:

cmd error log

我已经在互联网上研究了它但是找不到我使用BufferedReader的特殊情况的解决方案,因为同一个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();
    }
}

我正在此servlet中检索两个请求参数welcomeStoryTitlewelcomeStoryWords

以下是我的 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(){});
    };
}]);

我正在解析读者对象的错误,在第:

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

这可能是因为从请求中检索到字符串的大小吗?

1 个答案:

答案 0 :(得分:0)

'在0号位置'是赠品。您正在尝试解析空响应。