0

I am trying to access a database through a simple PHP code. But the constraints are that it is a remote database, accessed by a remote server from a real device.

here is a snippet of the code:-

ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
            postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));
            String response = null;
            try {
                response = CustomHttpClient.executeHttpPost("http://WebsiteName.com/check.php", postParameters);
                String res=response.toString();

                res= res.replaceAll("\\s+","");                              

               if(res.equals("1"))
                    error.setText("Correct Username or Password");
                else
                    error.setText("Sorry!! Incorrect Username or Password"); 
            } catch (Exception e) {
                un.setText(e.toString());
            }

The PHP is here. Most probably there is no problem with the PHP code because it works locally.

One of the most important things I want to clarify is where to keep the PHP code in the server. I have it directly under the root directory. When i execute it from the browser (say by typing website.com/check.php), i get the required response. Unfortunately many people who have given a tutorial on this, has mentioned about local server and local database more than remote ones. Is there something more I should do for getting it done with a real device?

PS: The app works fine when connected to an emulator and local database with local webserver.

===UPDATE===

I am pretty sure that the phone is not contacting the web server. I verified by making the webserver give a positive response always, which also failed. Any hints on where I should look at? I can't think of anything other than the online server...which is working (when checked from browser). Any way of cross checking it? Thanks.

4
  • 1
    so what exactly is the problem, it's not that clear to me Commented May 27, 2012 at 21:35
  • problem is that it is not working with a phone. I want to fix the code to run it on a phone. Frankly speaking i cant spot where exactly is the mistake. I posted it here so as to know if I am doing something wrong. Commented May 27, 2012 at 21:40
  • @gkris: When you say it doesn't work, what happens? Does the app crash, do you get an unexpected response, or does it just fail silently? Commented May 27, 2012 at 21:42
  • @xbonez: i always get the response as false. The app is supposed to check for 2 fields in the database and give positive response if user input matches the database entry. Commented May 27, 2012 at 22:05

1 Answer 1

3

This may or may not be the cause of the problem you are facing, but you must fix it either ways.

You are currently making the POST request synchronously. That means, as long as the POST request is executing and the device is awaiting a response, the phone and UI is locked up. Not only does this make for a bad user experience, if you lock up the UI thread for more than a certain amount of time, Android considers the application to have crashed.

Perhaps that is why it works when you make the request locally, because a POST request to your local webserver will be very quick as opposed to a remote webserver.

To make a HTTP request, asynchronously, look into Android AsyncTask

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

1 Comment

oh thanks a lot for this suggestion. I do get a request timed out error with the phone at times and this would be the reason for it. I will try to do the Async Task soon. Thanks again.

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.