2015-07-03 37 views
-1

我正在一个应用程序,我需要保存用户会话..我试图检索用户ID,但不知何故代码我已经实现似乎并不工作.. 我想,当用户打开应用程序..该应用程序应该打开从用户离开它..并只是添加新的东西,它..就像在Facebook应用程序..尝试检索SessionId并试图保存用户会话

这里的代码..帮我解决这个问题..这里使用一个随机的网址“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); 


     } 
    } 

回答