Take the 2-minute tour ×

I Have this JSP which just check the username and password and "out.write" 1 or 0 based on true or false.

        if(request.getParameter("username").equals("anas") && request.getParameter("password").equals("azeem"))

    %> </br>

Now my android code is somerhing like this

    protected void onCreate(Bundle savedInstanceState) {

        edit_username = (EditText) findViewById(R.id.edit_username);
        edit_password = (EditText) findViewById(R.id.edit_password);
        btn_login = (Button) findViewById(R.id.btnLogin);

        if (android.os.Build.VERSION.SDK_INT > 9) { // must add this code in
                            // order not to get the
                            // Exception while executing
                            // program
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()

        btn_login.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                username = edit_username.getText().toString();
                password = edit_password.getText().toString();

                Log.d(TAG, "Username:" + username);
                Log.d(TAG, "Password:" + password);

                try {
                    new Thread() {
                        public void run() {
                            try {
                                HttpClient client = new DefaultHttpClient();
                                HttpPost post = new HttpPost("");
                                List<NameValuePair> pairs = new ArrayList<NameValuePair>(2);
                                pairs.add(new BasicNameValuePair("username",edit_username.getText().toString()));
                                pairs.add(new BasicNameValuePair("password",edit_password.getText().toString()));
                                post.setEntity(new UrlEncodedFormEntity(pairs));
                                HttpResponse response = client.execute(post);
                                HttpEntity httpEntity = response.getEntity();
                                xml = EntityUtils.toString(httpEntity);
                                Log.d("xml", ""+xml.length());   //To confirm anything is there in the "xml"
                            } catch (Exception e) {

                } catch (Exception e) {
                    Log.d("xml", xml.toString());
                    Log.d("Server", e.toString());

                try {
                    if (Integer.parseInt(String.valueOf(xml.charAt(10))) == 1) {  //****HERE*******
                        Toast.makeText(getApplicationContext(), "LoginSuccessful",Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(Agent_Login.this,AgentHome.class);
                    } else {
                        Toast.makeText(getApplicationContext(),"Invalid Username or Password", Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    // TODO Auto-generated catch block


Now on HERE (in comment) there gives a NullPointerException, which is I think because the xml is empty.

So, my question is how can I get the reply from the JSP in Android. I tested it on PC with an HTML form and its working absolutely fine.

Any help would be greatly appreciated.

share|improve this question
add comment

1 Answer

  1. A varaible is named xml but it will probably not contain any XML, as far is I can see; you're returning HTML.
  2. Why would the 1 be located at position 10 in the returned string?
  3. You're logging the content of xml; what is its value?
share|improve this answer
First I must say that xml is a String variable at class level. –  user2044187 Apr 16 '13 at 7:52
First I must say that "xml" is a String variable at class level. A friend was using similar code and it was returning something like <html>1</html> or the 0 case istead of 1. That Log code doesn't run at all. –  user2044187 Apr 16 '13 at 7:55
@AnasAzeem You should look at JSON and use a JSON parser. Relying on the web server's response format in this hard-coded way is doomed to fail. Also, if you don't know what the content of xml is, just add a log call for it and look at it. –  Class Stacker Apr 16 '13 at 8:02
The xml is always Null, the code which logs it is never executed. Can you suggest some JSON tutorials, I know nothing about it. THANKS –  user2044187 Apr 16 '13 at 16:18
It's about time you add some log calls to see what goes wrong. Does the code even connect successfully to your page? –  Class Stacker Apr 16 '13 at 19:21
show 1 more comment

Your Answer


