-1

I am working on an app and i need to save the user session.. I am trying to retrieve the user id but somehow the code i've implemented doesn't seem to work.. I want that when the user opens the application.. the app should open from where the user left it..and just add new stuff to it.. like in the facebook app..

Here's the code.. help me fix it.. Using a random url here "https://www.coursera.org/"

public class HttpGetTask extends AsyncTask<Void, Void, String>
    {
        String playCount;

        private static final String eb = "https://www.coursera.org/";

        DefaultHttpClient mClient;

        @Override
        protected String doInBackground(Void... params) {
            // TODO Auto-generated method stub
             mClient = new DefaultHttpClient();
            HttpGet post1 = new HttpGet(eb);
            try
            {


                HttpResponse response = mClient.execute(post1);
                HttpEntity r_entity = response.getEntity();
                String xmlString = EntityUtils.toString(r_entity);
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder db  = factory.newDocumentBuilder();
                InputSource instream = new InputSource();
                instream.setCharacterStream(new StringReader(xmlString));
                org.w3c.dom.Document doc = db.parse(instream);  


                 playCount = "sessionid";
                    NodeList nl = doc.getElementsByTagName("playCount");
                    for(int i = 0;i < nl.getLength();i++)
                    {
                        if(nl.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE)
                        {
                            org.w3c.dom.Element nameElement = (org.w3c.dom.Element) nl.item(i);
                            playCount = nameElement.getFirstChild().getNodeValue().trim();
                        }
                    }
            }
            catch(ClientProtocolException e)
            {
                e.printStackTrace();
            }
            catch(DOMException e)
            {
                e.printStackTrace();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
            catch(ParserConfigurationException e)
            {
                e.printStackTrace();
            }
            catch(SAXException e)
            {
                e.printStackTrace();
            }

            return playCount;
        }

        @Override
        protected void onPostExecute(String result) {

            SharedPreferences preferences = getSharedPreferences("com.example.tabsletssee", Context.MODE_PRIVATE);

            preferences.edit().putString("Session",playCount).commit();

            preferences.getString("Session", "");
            super.onPostExecute(result);


        }
    }  
Chandrakanth
  • 3,711
  • 2
  • 18
  • 31

1 Answers1

0

Use SyncAdapter For Offline data storage

https://developer.android.com/training/sync-adapters/creating-sync-adapter.html

Munish Kapoor
  • 3,141
  • 2
  • 26
  • 41