I have another application built in iOS, and when the user logs into the app the server logs them in on that session. In the iOS app the session is kept alive as long as normal sessions and all requests from the app see the person still logged in.
I assumed this would be the same with the Android app, but after logging in I switch to the next activity, then send a request to the server to populate the page. The result I'm getting back is saying the user isn't logged in.
Is there a difference here that I'm not understanding that is not keeping my session on the server alive?
I have a base class that all my Activities extend, and inside the base class I have this protected class. I use it to send all the requests.
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);
}
}