简体   繁体   English

从Android访问WAMP上的mySQL

[英]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. 我无法理解如何准确配置WAMP,以便Android应用程序对我使用它创建的SQL数据库进行远程(而非LAN)访问。 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. 我知道我不应该直接从应用程序连接到数据库,而应该使用PHP脚本来查询数据库并返回结果。 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. 我不了解的是如何将所有这些组合在一起,如何在哪里放置PHP脚本,如何访问它们,如何允许并连接到Web服务器以从数据库中获取数据。

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. 我已经跟踪并搜索了很多关于此的教程,但是它们似乎都只在本地主机中使用wamp处理,我可以在本地成功访问数据,但是我对如何设置所有内容感到困惑,以便可以从中查询数据库我的android设备上的任何互联网连接。

Any advice on how to achieve this would be very much appreciated. 任何有关如何实现这一目标的建议将不胜感激。

You have to upload your php script to your server : 您必须将php脚本上传到服务器:

Ex : Your php is in : myserver.com/myscript.php 例如:您的php位于:myserver.com/myscript.php

In your Android app, You post your variables, and you connect your php script with HttpClient : 在您的Android应用中,您发布变量,然后将php脚本与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 : 在您的php中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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