小弟有个需求就是,在有网络的情况下会 post数据与服务器交互。
如果当前网络异常,则把 post 的 josn数据保存到sqlite
当网络正常的时候则从sqlite中把json拿出来post到服务器
list.add(json.toString());
如过list.size() 超过一定数量,比如说100的时候。要怎么post?
小弟我现在是 起一个线程来执行AsyncTask post的。
for(int i =0 ; i < list.size();i++){
Task task = new Task();
task.execute(list.get(i));
}
private class Task extends AsyncTask<String,String,Boolean>{
@Override
protected Boolean doInBackground(Void... params) {
// post data to server....
return false;
}
}
各位是如何执行N多个post的?
------解决思路----------------------
為什麼要起那麼多線程 ? 能不能開一個線程,再一個個的依序處理相關 json ?像…
Runnable work = new Runnable() {
@Override
run() {
int size = list.size();
for(int i =0 ; i < size; i++) {
// post data to server
}
}
};
Thread thread = new Thread(work).start();
然後其實可以用線程池(ThreadPool),如果你 post data 優先級不是那麼高的話~
有錯請指正 :D
------解决思路----------------------
试试用线程池吧 或者 libcurl吧
------解决思路----------------------
你用AsyncTask挺好的,,相当于一个线程池了。
public final AsyncTask<Params, Progress, Result> executeOnExecutor (Executor exec, Params... params)
Added in API level 11
Executes the task with the specified parameters. The task returns itself (this) so that the caller can keep a reference to it.
This method is typically used with THREAD_POOL_EXECUTOR to allow multiple tasks to run in parallel on a pool of threads managed by AsyncTask, however you can also use your own Executor for custom behavior.