Я разрабатываю приложение для Android, цель которого — отправить некоторые данные на сервер. Однако иногда соединение Wi-Fi может отсутствовать, поэтому я хотел бы спросить, можно ли создать кеш для хранения нескольких наборов данных, скажем, 3 наборов, а затем приложение будет автоматически отправлять эти данные, когда соединение доступно.
Вот как я недавно отправляю свои данные на сервер:
private class GrabURL extends AsyncTask{
//ArrayList object for storing the string pairs
ArrayList nameValuePairs;
public GrabURL() {
//constructor of the class
nameValuePairs = new ArrayList();
}
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();
}
}
Подробнее здесь: https://stackoverflow.com/questions/117 ... pplication