使用PHP会用我的Andr​​oid应用程序登录 [英] Using PHP sessions with my android application to login

查看:125
本文介绍了使用PHP会用我的Andr​​oid应用程序登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个登录脚本为我的Andr​​oid应用程序,该脚本将发送电子邮件地址和密码到PHP服务器,验证登录,然后创建一个PHP会话,使用户处于登录。这是我的code,

I am trying to make a login script for my android application, the script will send my email and password to the PHP server, verify the login and then create a PHP session so that the user stays logged in. This is my code,

HttpPost httppost = new HttpPost("http://server.com/login.php");
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();

public String login() {

    String userID = "";

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("email", "e@e.com"));
        nameValuePairs.add(new BasicNameValuePair("password", "admin"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        userID = EntityUtils.toString(response.getEntity());
        //Log.v("Login response", "" + userID);



    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

    return userID;
} 

该脚本成功地将数据发送到我的服务器和我的PHP成功登录的用户。我已经把HttpClient的HttpClient的=新DefaultHttpClient();外我主登录方法。这有助于存储会话,直到我呼吁另一个类,那么它只是再次复位会话。所以,我想知道我怎么可以改变code,使HttpClient的以某种方式保存这样我就可以保持会话并随时登录到我的服务器。谢谢!

This script successfully sends data to my server and my PHP successfully logs the user on. I have placed "HttpClient httpclient = new DefaultHttpClient();" outside my main login method. This has helped store the session until I call upon another class, then it just resets the session again. So I am wondering how I can alter the code so that "httpclient" is somehow stored so I can keep the session and stay logged into my server. Thank you!

推荐答案

Android的HTTP GET会话Cookie

获取该cookie的会话ID,并使用该Cookie中的下一个请求给服务器。

Get the cookie session ID and use that cookie in the next requests to the server.

这篇关于使用PHP会用我的Andr​​oid应用程序登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆