简体   繁体   English

Android将PHP链接到MySQL以登录应用程序

[英]Android link PHP to MySQL to login in app

I'm trying to understand how can I link by Android App to PHP/MySQL in order to get a login from a user that is already on a database. 我试图了解如何通过Android App链接到PHP / MySQL,以便从已经在数据库中的用户获得登录名。

I found that one way of doing it was through going to PHP pages, sending the inputed data from the app and post it on parameters on PHP, and PHP itself should look for the user references and return if the user either exists or not. 我发现一种方法是通过转到PHP页面,从应用程序发送输入的数据并将其发布到PHP的参数上,PHP本身应查找用户引用,并在用户存在或不存在时返回。

However, every place I look for how to do this it is too much either complicated or not well explained. 但是,我所寻找的每个地方如何做到这一点,要么过于复杂,要么没有得到很好的解释。 So I'm looking for a way to do this, and if you know some tutorial or well commented code that you could provide for me to learn it would be very useful to me. 因此,我正在寻找一种方法来执行此操作,如果您知道一些可以提供给我学习的教程或注释良好的代码,对我来说将非常有用。 Or any of you could teach me how to do it or where to start learning would be great. 或者你们中的任何一个都可以教我如何做或在哪里开始学习会很棒。

Thank you very much in advance. 提前非常感谢您。

For this kind of interaction I use this code: 对于这种交互,我使用以下代码:

public static String httpPost(String address, List<NameValuePair> parameters) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(address);

    InputStream inputStream = null;
    BufferedReader reader = null;
    StringBuilder sb = new StringBuilder();

    try {
        httpPost.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();

        inputStream = entity.getContent();
        reader = new BufferedReader((new InputStreamReader(inputStream)));

        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }

    }catch (Exception e) {
        e.printStackTrace();
    }finally {
        try {
            if (reader != null)
                reader.close();
            if (inputStream != null)
                inputStream.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    return sb.toString();
}

You have to pass this function a String which is the URL of the PHP script, and a list of NameValuePair s containing the data you want to pass to the script, and it returns the answer from the server. 您必须向此函数传递一个String (即PHP脚本的URL)和一个NameValuePair列表,其中包含要传递给脚本的数据,并且该列表从服务器返回答案。

If you want to use it for login, you can use something like that: 如果要使用它进行登录,则可以使用类似以下的方式:

public boolean login(String username, String passwd) {
    List<NameValuePair> parameters = new ArrayList<NameValuePair>();
    parameters.add(new BasicNameValuePair("username", username);
    parameters.add(new BasicNameValuePair("password", passwd);

    String result = httpPost("http://test.com/login.php", parameters);

    return result.equals("ok");
}

There are already answers mentioning about posting Input data to your PHP web page. 已经有关于将输入数据发布到PHP网页的答案。 How ever you can also follow another approach. 您还可以如何遵循另一种方法。

In this approach you will need a webview loaded initially(assuming login is needed for all activities). 在这种方法中,您需要首先加载一个Webview(假设所有活动都需要登录)。 In the web view simply load the web page, say http://www.example.com/login?device=mobile Once user has logged in successfully, assuming that website would load a logged in page, example.com/user you need to check if this was the URL which is opened. 在Web视图中,只需加载网页,例如http://www.example.com/login?device=mobile device http://www.example.com/login?device=mobile mobile用户成功登录后,假设网站将加载您需要的登录页面example.com/user检查这是否是打开的URL。 Then it means that login was completed successfully. 这意味着登录成功完成。 Then you could proceed to next activity in your App. 然后,您可以继续进行App中的下一个活动。

Reference 参考

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

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