我在iOS中創建了另一個應用程序,當用戶登錄到應用程序時,服務器在該會話中記錄它們。在iOS應用程序中,只要正常會話和應用程序發出的所有請求都保持活躍狀態,則會話仍然保持活躍狀態。Android:使活動在遠程服務器上保持活動狀態
我認爲這與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);
}
}
我用更多的細節更新了我的問題。 – Jhorra
我更新了我的答案和更多細節。 – digitaljoel
完美,謝謝。 – Jhorra