在 android 中维护会话(应用程序在服务器端保持身份验证) [英] Maintaining session in android ( application stay authenticated on the server side)

查看:18
本文介绍了在 android 中维护会话(应用程序在服务器端保持身份验证)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 android 中构建一个登录应用程序,其中我正在点击一个 url(使用用户名和密码)直到该部分它工作正常,但之后每当我点击一个 url(一旦用户通过身份验证),它什么都不返回(即错误信息,如请先登录).然而,它在非常相似的 iPhone 应用程序和浏览器上运行良好.

I am building a login application in android in which i am hitting a url(with username and password) upto that part it works fine but after that whenever I am hitting a url(once the user is authenticated) , it return nothing(i.e. a error message like please login first). However it works fine in very similar iphone app and on browser.

我发现这是 phpSessionId 的错误(即会话被销毁以供进一步请求),如果我们希望我们的 Android 应用程序在服务器端保持身份验证,我们需要在第一次连接后获取该 ID,然后发送它在我们所有后续请求的标头中.

I got somewhere that it is the error of phpSessionId(i.e. the session is destroyed for further request) and If we want our Android application to stay authenticated on the server side we need to fetch that id after the first connection and then send it in the headers of all our subsequent requests.

但问题是我无法从第一个连接的标头中获取 sessionId 并将其与进一步的请求一起发送.

But the problem is that I am unable to get the sessionId from header of the first connection and sending it with further request along with the header.

请给我一些代码或链接以正确完成任务.谢谢.

Please give me some codes or links to complete the task properly. Thanks.

推荐答案

我终于解决了Android 中会话处理的问题.Android 不能处理会话本身(一个简单的浏览器可以)所以我们必须明确地处理它.我稍微更改了 http 连接的代码.建立连接时,在第一个 Activity 中创建了一个 DefaultHttpClient 实例.

Finally I solved the issue of session handling in Android. Android cant handle the session itself(which a simple browser can) so we have to handle it explicitly. I changed the code for http connection a bit. Created an instance of DefaultHttpClient in the first Activity when connection established.

public static DefaultHttpClient httpClient;

第一次连接,我做了以下:

For the first time connection,I did the following:

URL url=new URL(urlToHit);
LoginScreen.httpClient = new DefaultHttpClient(); //LoginScreen is the name of the current Activity

HttpPost httppost = new HttpPost(url.toString());
HttpResponse response = LoginScreen.httpClient.execute(httppost); 

xr.parse(new InputSource(url.openStream())); //SAX parsing

现在对于所有进一步的连接,我使用了相同的 httpClient例如在下一个活动中:

Now for all further connections I used the same httpClient For example in the next activity:

URL url=new URL(urlToHit);

HttpPost httppost = new HttpPost(url.toString());
HttpResponse response = LoginScreen.httpClient.execute(httppost); 

// Log.v("response code",""+response.getStatusLine().getStatusCode());

// Get hold of the response entity
HttpEntity entity = response.getEntity();

InputStream instream = null;

if (entity != null) {
    instream = entity.getContent();
}
xr.parse(new InputSource(instream)); //SAX parsing

希望这也能帮助大家解决Android 中的会话问题.

Hope this will help you all too to solve session issue in Android.

这篇关于在 android 中维护会话(应用程序在服务器端保持身份验证)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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