Maintaining session in android ( application stay authenticated on the server side)(在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中维护会话(应用程序在服务器端保持身份验证)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在android中维护会话(应用程序在服务器端保持身份验证)
基础教程推荐
- Android - 如何在runOnUiThread 中将数据传递给Runnable? 2022-01-01
- UINavigationItem 的持久 rightBarButtonItem 属性 2022-01-01
- 在视图控制器的宽度上水平均匀分布 UIButton 的最简单方法? 2022-01-01
- EditText 中的 setHintTextColor() 2022-01-01
- UINavigationBar 隐藏按钮文本 2022-01-01
- 在 iOS 7 下 CCMenu 错位 2022-01-01
- Firebase 云消息传递令牌未生成 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 从 UIWebView 访问元数据 2022-01-01
- 更改 UITableView 部分标题的颜色 2022-01-01