[英]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代码可以与其他请求一起正常工作,但是在这里,我得到了以下错误:
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中检索两个请求参数
welcomeStoryTitle
和welcomeStoryWords
。
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.