【发布时间】:2015-07-19 09:04:05
【问题描述】:
我正在将 json 从 servlet 发送到 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 运行得很好并返回 json 即
long uname = 48372984;
String pwd = "fabcd"
输出 -
{"login":"Fail"}
【问题讨论】:
-
您是否阅读过异常...显然不是...来自服务器的响应(
Apache ...) 不是json 字符串 ... -
我读得很好,我知道在这种情况下它没有返回 json,我只是不知道问题出在哪里。这就是为什么我在直接赋值时给出示例,然后它返回json。
-
我假设您在 android 应用程序中遇到异常?
异常中的 Apache 在将请求发布到服务器时看起来像是收到了一些 Apache 错误页面。 -
@Selvin 我知道 (
Apache...) 不是 json ,我只是在响应中看不到它来自哪里 -
尝试在浏览器中调用您的servlet,并将“mobile”和“pass”参数作为get 参数发布...看看您是否获得Json 或errorpage。如果出现错误页面,请检查 http 日志。
标签: java android json servlets jsonexception