Android:在活动之间保持远程服务器上的会话活动
我有另一个内置iOS的应用程序,当用户登录到该应用程序时,服务器会将他们登录到该会话中。在iOS应用程序中,会话与正常会话一样保持活动状态,并且来自应用程序的所有请求都可以看到仍然登录的用户 我认为这与Android应用程序相同,但在登录后,我切换到下一个活动,然后向服务器发送请求以填充页面。我得到的结果是用户没有登录 这里是否有一个我不理解的区别,那就是不能使我在服务器上的会话保持活动状态 我有一个所有活动都扩展的基类,在基类中我有一个受保护的类。我用它来发送所有的请求Android:在活动之间保持远程服务器上的会话活动,android,session,Android,Session,我有另一个内置iOS的应用程序,当用户登录到该应用程序时,服务器会将他们登录到该会话中。在iOS应用程序中,会话与正常会话一样保持活动状态,并且来自应用程序的所有请求都可以看到仍然登录的用户 我认为这与Android应用程序相同,但在登录后,我切换到下一个活动,然后向服务器发送请求以填充页面。我得到的结果是用户没有登录 这里是否有一个我不理解的区别,那就是不能使我在服务器上的会话保持活动状态 我有一个所有活动都扩展的基类,在基类中我有一个受保护的类。我用它来发送所有的请求 protected c
protected class API extends AsyncTask <Void, Void, String>
{
public String RequestName;
public String RequestType;
public String RequestUrl;
public List<NameValuePair> RequestParameters;
public API(String Name, String Type, String Url, List<NameValuePair> params)
{
RequestName = Name;
RequestType = Type;
RequestUrl = Url;
RequestParameters = params;
}
protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException
{
InputStream in = entity.getContent();
StringBuffer out = new StringBuffer();
int n = 1;
while (n>0)
{
byte[] b = new byte[4096];
n = in.read(b);
if (n>0)
out.append(new String(b, 0, n));
}
return out.toString();
}
@Override
protected String doInBackground(Void... params)
{
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
String text = null;
if(RequestType == "post")
{
HttpPost p = new HttpPost("http://www.ehrx.com/api/" + RequestUrl);
try
{
p.setEntity(new UrlEncodedFormEntity(RequestParameters));
HttpResponse response = httpClient.execute(p, localContext);
HttpEntity entity = response.getEntity();
text = getASCIIContentFromEntity(entity);
}
catch (Exception e)
{
return e.getLocalizedMessage();
}
}
else
{
HttpGet httpGet = new HttpGet("http://www.ehrx.com/api/" + RequestUrl);
try
{
HttpResponse response = httpClient.execute(httpGet, localContext);
HttpEntity entity = response.getEntity();
text = getASCIIContentFromEntity(entity);
}
catch (Exception e)
{
return e.getLocalizedMessage();
}
}
return text;
}
protected void onPostExecute(String results)
{
HandleResult(results, RequestName);
}
}
我猜会话在服务器上仍然是活动的,但是您在android应用程序中丢失了会话cookie。看起来您正在为每个请求创建一个新的客户端和一个新的上下文。您需要为上下文设置cookie存储,以便它可以使用您之前已验证的请求中的会话cookie。的答案应该会给您提供更多信息。我猜会话在服务器上仍然有效,但您在android应用程序中丢失了会话cookie。看起来您正在为每个请求创建一个新的客户端和一个新的上下文。您需要为上下文设置cookie存储,以便它可以使用您之前已验证的请求中的会话cookie。的答案应该会给您提供更多信息。使用单例类可以轻松解决此问题。您应该在所有HTTP请求中使用相同的HTTP客户端。使用单例类可以轻松解决此问题。您应该在所有HTTP请求中使用相同的HTTP客户端
protected class API extends AsyncTask <Void, Void, String>
{
public String RequestName;
public String RequestType;
public String RequestUrl;
public List<NameValuePair> RequestParameters;
public API(String Name, String Type, String Url, List<NameValuePair> params)
{
RequestName = Name;
RequestType = Type;
RequestUrl = Url;
RequestParameters = params;
}
protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException
{
InputStream in = entity.getContent();
StringBuffer out = new StringBuffer();
int n = 1;
while (n>0)
{
byte[] b = new byte[4096];
n = in.read(b);
if (n>0)
out.append(new String(b, 0, n));
}
return out.toString();
}
@Override
protected String doInBackground(Void... params)
{
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
String text = null;
if(RequestType == "post")
{
HttpPost p = new HttpPost("http://www.ehrx.com/api/" + RequestUrl);
try
{
p.setEntity(new UrlEncodedFormEntity(RequestParameters));
HttpResponse response = httpClient.execute(p, localContext);
HttpEntity entity = response.getEntity();
text = getASCIIContentFromEntity(entity);
}
catch (Exception e)
{
return e.getLocalizedMessage();
}
}
else
{
HttpGet httpGet = new HttpGet("http://www.ehrx.com/api/" + RequestUrl);
try
{
HttpResponse response = httpClient.execute(httpGet, localContext);
HttpEntity entity = response.getEntity();
text = getASCIIContentFromEntity(entity);
}
catch (Exception e)
{
return e.getLocalizedMessage();
}
}
return text;
}
protected void onPostExecute(String results)
{
HandleResult(results, RequestName);
}
}