值 <html><head><title>Apache 类型 java.lang.String 无法转换为 JSONObject
Value <html><head><title>Apache of type java.lang.String cannot be converted to JSONObject
我正在从 servlet 发送 json 到 android 应用程序,但出现以下异常:-
org.json.JSONException: Value <html><head><title>Apache of type java.lang.String cannot be converted to JSONObject
以下是我的servlet代码,如有不妥请指正:-
public class LoginCheck extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/json;charset=UTF-8");
PrintWriter out = response.getWriter();
JSONObject obj1 = new JSONObject();
long uname =Long.parseLong(request.getParameter("mobile"));
String pwd = request.getParameter("pass");
try {
Connection con = new MyConnection().connect();
PreparedStatement ps = con.prepareStatement("select * from bmt_user where mobile_num=? and password=?");
ps.setLong(1,uname);
ps.setString(2,pwd);
ResultSet rs=ps.executeQuery();
if(rs.next())
{
obj1.accumulate("login","Success");
out.println(obj1.toString());
}
else
{
obj1.accumulate("login","Fail");
out.println(obj1.toString());
}
out.write(obj1.toString());
}catch(Exception e){out.println(e.toString());}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
另外,当我直接将值分配给 uname 和 pwd 时,不使用 request.getParameter() ,servlet 运行得很好并且 returns json 即
long uname = 48372984;
String pwd = "fabcd"
输出-
{"login":"Fail"}
您的 servlet 代码抛出了一个未捕获的异常,该异常最终出现在 HTML 风格的服务器默认 HTTP 500 错误页面中,在 Apache Tomcat 服务器的情况下具有以下 header:
<!DOCTYPE html><html><head><title>Apache Tomcat/8.0.21 - Error report</title>
这就解释了为什么客户端的 JSON 解析器会触发它。尝试在普通网络浏览器中重现相同内容,您将看到完整的 HTML 错误页面。
很可能问题出在下面一行:
long uname = Long.parseLong(request.getParameter("mobile"));
你无处可去 prechecking/catching 一个潜在的 NullPointerException
和 NumberFormatException
这里。阅读服务器日志并相应地修复代码。也许参数名称错误?您的局部变量名为 uname
,与请求参数名称 mobile
.
不匹配
我正在从 servlet 发送 json 到 android 应用程序,但出现以下异常:-
org.json.JSONException: Value <html><head><title>Apache of type java.lang.String cannot be converted to JSONObject
以下是我的servlet代码,如有不妥请指正:-
public class LoginCheck extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/json;charset=UTF-8");
PrintWriter out = response.getWriter();
JSONObject obj1 = new JSONObject();
long uname =Long.parseLong(request.getParameter("mobile"));
String pwd = request.getParameter("pass");
try {
Connection con = new MyConnection().connect();
PreparedStatement ps = con.prepareStatement("select * from bmt_user where mobile_num=? and password=?");
ps.setLong(1,uname);
ps.setString(2,pwd);
ResultSet rs=ps.executeQuery();
if(rs.next())
{
obj1.accumulate("login","Success");
out.println(obj1.toString());
}
else
{
obj1.accumulate("login","Fail");
out.println(obj1.toString());
}
out.write(obj1.toString());
}catch(Exception e){out.println(e.toString());}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
另外,当我直接将值分配给 uname 和 pwd 时,不使用 request.getParameter() ,servlet 运行得很好并且 returns json 即
long uname = 48372984;
String pwd = "fabcd"
输出-
{"login":"Fail"}
您的 servlet 代码抛出了一个未捕获的异常,该异常最终出现在 HTML 风格的服务器默认 HTTP 500 错误页面中,在 Apache Tomcat 服务器的情况下具有以下 header:
<!DOCTYPE html><html><head><title>Apache Tomcat/8.0.21 - Error report</title>
这就解释了为什么客户端的 JSON 解析器会触发它。尝试在普通网络浏览器中重现相同内容,您将看到完整的 HTML 错误页面。
很可能问题出在下面一行:
long uname = Long.parseLong(request.getParameter("mobile"));
你无处可去 prechecking/catching 一个潜在的 NullPointerException
和 NumberFormatException
这里。阅读服务器日志并相应地修复代码。也许参数名称错误?您的局部变量名为 uname
,与请求参数名称 mobile
.