0

I am sending json from servlet to android application , and the following exception occurs : -

 org.json.JSONException: Value <html><head><title>Apache of type java.lang.String cannot be converted to JSONObject

Following is my servlet code , please correct me if anything is wrong here :-

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);
  }

 }

Plus , when i assign the values to uname and pwd directly , without using request.getParameter() , the servlet runs just fine and returns json i.e

long uname = 48372984;
String pwd = "fabcd"

output -

{"login":"Fail"}
10
  • 1
    Did you read an exception ... ovbiously not ... response from the server(<html><head><title>Apache...) is not a json string ... Commented May 8, 2015 at 12:37
  • 1
    i read it just fine , i know it is not returning json in the case, i just couldn't figure out where the problem is. That is why i gave example when i assign values directly , it returns json then . Commented May 8, 2015 at 12:39
  • 2
    I assume you get the exception in the android application? <html><head><title>Apache in the exception looks like you get some Apache error page when posting your request to the server. Commented May 8, 2015 at 12:43
  • 1
    @Selvin and i know (<html><head><title>Apache...) is not json , i just can't see where it is coming from in the response Commented May 8, 2015 at 12:44
  • 1
    try calling your servlet in a browser, and post the "mobile" and "pass" parameters as get parameters... see if you get Json or errorpage. In the event of an error page, check http logs. Commented May 8, 2015 at 12:46

1 Answer 1

2

Your servlet code threw an uncaught exception which ended up in server default HTTP 500 error page in HTML flavor which has in case of Apache Tomcat server the below header:

<!DOCTYPE html><html><head><title>Apache Tomcat/8.0.21 - Error report</title>

That explains why the JSON parser in the client side flipped on it. Try reproducing the same in a normal webbrowser and you'll see the HTML error page in its entirety.

Most likely the problem is at the below line:

long uname = Long.parseLong(request.getParameter("mobile"));

You're nowhere prechecking/catching a potential NullPointerException and NumberFormatException here. Read the server logs and fix the code accordingly. Perhaps the parameter name is wrong? Your local variable is named uname which doesn't sensibly match the request parameter name mobile.

Sign up to request clarification or add additional context in comments.

6 Comments

Accurate !! But previously i made a servlet , with the same parsing statement , which executes fine .
If it did in the current servlet, you wouldn't have asked this question :) Moreover, it would still fail when a client sent a bad request with wrong parameter values. You should have returned a HTTP 400 error then.
true , but i am passing a long data type only , and dont worry about the sense , its correct .
The answer is in the actual exception+trace in the server log. Go read it.
Thanks , got it solved by reading the server log . Looks like a null value was being passed and the problem was in the client side , i was passing a variable in the BasicNameValuePair Changed the following code in android : - mob = et1.getText().toString() ; userLogin.add(new BasicNameValuePair("mobile",mob)); to userLogin.add(new BasicNameValuePair("uname",et1.getText().toString()));
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.