为android应用程序创建缓存

为android应用程序创建缓存,android,caching,Android,Caching,我正在开发一个android应用程序,目的是向服务器发送一些数据。但是,有时可能没有wifi连接,所以我想问一下,是否可以创建一个缓存来存储多组数据,比如说3组,然后应用程序会在连接可用时自动发送这些数据 以下是我最近向服务器发送数据的方式: private class GrabURL extends AsyncTask<String, Void, Void>{ //ArrayList object for storing the string pairs Array

我正在开发一个android应用程序,目的是向服务器发送一些数据。但是,有时可能没有wifi连接,所以我想问一下,是否可以创建一个缓存来存储多组数据,比如说3组,然后应用程序会在连接可用时自动发送这些数据

以下是我最近向服务器发送数据的方式:

private class GrabURL extends AsyncTask<String, Void, Void>{
    //ArrayList object for storing the string pairs
    ArrayList<NameValuePair> nameValuePairs;

    public GrabURL() { 
        //constructor of the class
        nameValuePairs = new ArrayList<NameValuePair>(); 
      } 


    protected void onPreExecute(String key, String value) {
        //store the pair of values into the ArrayList 
        nameValuePairs.add(new BasicNameValuePair(key,value));
        }

    @Override
    protected Void doInBackground(String... urls) {
        // TODO Auto-generated method stub
        //Operation being executed in another thread
        try{
            //set up the type of HTTPClient
            HttpClient client = new DefaultHttpClient();
            //set up the location of the server
            HttpPost post = new HttpPost(urls[0]);
            //translate form of pairs to UrlEncodedFormEntity 
            UrlEncodedFormEntity ent = new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8);
            //set up the entity being sent by post method
            post.setEntity(ent);
            //execute the url and post the values
            //client.execute(post);
            HttpResponse responsePOST = client.execute(post); 
            HttpEntity resEntity = responsePOST.getEntity();
            line = EntityUtils.toString(resEntity);
         } catch (Exception e) {
             //catch the exception
            line = "Can't connect to server";
         }
        return null;
    }

    protected void onPostExecute(Void unused) {
        Toast.makeText(getApplicationContext(), "Value updated", Toast.LENGTH_SHORT).show();
    }
}
私有类GrabURL扩展异步任务{
//用于存储字符串对的ArrayList对象
ArrayList名称值对;
公共抓取URL(){
//类的构造函数
nameValuePairs=新的ArrayList();
} 
受保护的void onPreExecute(字符串键、字符串值){
//将这对值存储到ArrayList中
添加(新的BasicNameValuePair(键,值));
}
@凌驾
受保护的Void doInBackground(字符串…URL){
//TODO自动生成的方法存根
//正在另一个线程中执行的操作
试一试{
//设置HTTPClient的类型
HttpClient=new DefaultHttpClient();
//设置服务器的位置
HttpPost=新的HttpPost(URL[0]);
//将对的形式转换为UrlEncodedFormEntity
UrlEncodedFormEntity ent=新的UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8);
//设置通过post方法发送的实体
邮政实体(ent);
//执行url并发布值
//客户。执行(post);
HttpResponse responsePOST=client.execute(post);
HttpEntity当前状态=responsePOST.getEntity();
行=EntityUtils.toString(最近性);
}捕获(例外e){
//抓住例外
line=“无法连接到服务器”;
}
返回null;
}
受保护的void onPostExecute(未使用的void){
Toast.makeText(getApplicationContext(),“值更新”,Toast.LENGTH\u SHORT.show();
}
}

我认为缓存数据很简单,您可以将其存储在内存中,也可以将其存储为文件。您可以侦听网络状态更改事件,以便在连接可用时收到通知


这是你的电话号码

我认为缓存数据很简单,您可以将其存储在内存中,也可以将其存储为文件。您可以侦听网络状态更改事件,以便在连接可用时收到通知


这是你的电话号码

这可能有助于为对象+图像缓存创建抽象缓存库


这可能有助于为对象+图像缓存创建抽象缓存库


要存储原始内容,您只需使用SharedReferences,一旦网络可用,您就可以检查SharedReferences中是否存在具有特定键的内容。如果不在持久性环境(如SharedReferences、Sqlite、SD卡内文件或内部存储器等)中写入数据,关闭应用程序将导致数据丢失。

要存储原始内容,只需使用SharedReferences,一旦网络可用,您可以检查SharedReferences中是否存在具有特定键的内容。如果您不在持久性环境(如SharedReferences、Sqlite、SD卡内文件或内部存储器等)中写入数据,关闭应用程序将导致数据丢失。

因此,我可以使用示例代码确定是否存在连接,如果不存在连接,则让程序等待。实际上,我想做的是类似whatsapp的事情,所以当没有internet连接时,它将首先缓存数据,然后它可以发送数据,即使我已返回到主页。这是一个不同的问题。实际上,您可以使用服务来侦听示例代码中的网络状态更改事件。并在连接可用时发送数据。即使你回到家里,服务也会在后台运行。所以我可以利用示例代码来确定是否存在连接,如果没有连接,让程序等待。实际上,我想做的是类似whatsapp的事情,所以当没有internet连接时,它将首先缓存数据,然后它可以发送数据,即使我已返回到主页。这是一个不同的问题。实际上,您可以使用服务来侦听示例代码中的网络状态更改事件。并在连接可用时发送数据。即使您回到家中,该服务也将在后台运行。