我正在开发一个应用程序,我需要保存用户会话..我正在尝试检索用户ID但不知何故,我实现的代码似乎不起作用.. 我希望当用户打开应用程序时...应用程序应该从用户离开的地方打开...并且只是添加新内容...就像在Facebook应用程序中一样..
这是代码..帮我修复它..在这里使用随机网址“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);
}
}
答案 0 :(得分:0)
使用SyncAdapter进行离线数据存储
https://developer.android.com/training/sync-adapters/creating-sync-adapter.html