我在 iOS 中构建了另一个应用程序,当用户登录该应用程序时,服务器将他们登录到该会话中。在 iOS 应用程序中,只要正常会话和来自应用程序的所有请求看到该人仍然登录,会话就会保持活动状态。
我认为这与 Android 应用程序相同,但登录后我切换到下一个活动,然后向服务器发送请求以填充页面。我回来的结果是说用户没有登录。
这里有什么我不明白的区别是不是让我的服务器上的会话保持活动状态?
我有一个我的所有活动都扩展的基类,在基类中我有这个受保护的类。我用它来发送所有请求。
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);
}
}