简体   繁体   中英

Accessing mySQL on WAMP from Android

I am having trouble understanding how exactly to configure WAMP for remote (not LAN) access by an Android app to the SQL database that I have created with it. I understand that I shouldn't be directly connecting to the database from the app but use a PHP script to query the database and return the results. What I am not understanding is how all this fits together, Where do I put the PHP scripts, How do I access them, How do I allow and connect to the webserver to get the data out of the database.

I have followed and searched for lots of tutorials on this but they all seem to deal with only using wamp in localhost, I can successfully access the data locally but I am stumped with how to set all that up so that I could query the database from any internet connection on my android device.

Any advice on how to achieve this would be very much appreciated.

You have to upload your php script to your server :

Ex : Your php is in : myserver.com/myscript.php

In your Android app, You post your variables, and you connect your php script with HttpClient :

    URL_TO_YOUR_SCRIPT = "myserver.com/myscript.php";
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("userId", userId));

    HttpClient httpclient = new DefaultHttpClient();
    // specify the URL you want to post to
    HttpPost httppost = new HttpPost(URL_TO_YOUR_SCRIPT);
    try {
        // create a list to store HTTP variables and their values
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
        HttpResponse response = httpclient.execute(httppost);
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == HttpURLConnection.HTTP_OK) {
            HttpEntity getResponseEntity = response.getEntity();
            return getResponseEntity.getContent();

        } else {
            Log.e("ERROR", statusLine.getStatusCode() + "");
            HttpEntity getResponseEntity = response.getEntity();
            return getResponseEntity.getContent();
        }

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

In your php :

<?
    $userId=$_REQUEST['userId'];
    ....
   // in general you return a json string to get status and result
?>

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