简体   繁体   中英

Connecting App with PHP and mysql

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM