我對Async Task瞭解得不多,所以我很抱歉如果我的問題太基本了。
我有一個AsyncTask類,它從我的數據庫下載一些數據並在列表視圖中顯示它們..
現在我的包含執行任務的碎片將顯示每次我的碎片重新啓動時加載進度,這有點令人討厭..
所以我決定這樣做:保存異步任務結果
下載任務,我的片段開始(在setUserVisibleHint方法),保存結果,並從那裏向他們展示我的列表視圖.. 並重復每次重新啓動應用程序這個.. 但我不知道該怎麼做,如果你能給我一點提示,我會很感激。
這是我的AsyncTask類:
public class RingBankAsyncTask extends AsyncTask<String, Void, String> {
private ProgressDialog progressDialog;
private AsyncResponse delegate;
private Context context;
private HashMap<String, String> postData = new HashMap<String, String>();
private String loadingMessage = "Loading...";
private boolean showLoadingMessage = true;
public RingBankAsyncTask(Context context, AsyncResponse delegate) {
this.delegate = delegate;
this.context = context;
}
public RingBankAsyncTask(Context context, boolean showLoadingMessage, AsyncResponse delegate) {
this.delegate = delegate;
this.context = context;
this.showLoadingMessage = showLoadingMessage;
}
public RingBankAsyncTask(Context context, HashMap<String, String> postData, AsyncResponse delegate) {
this.context = context;
this.postData = postData;
this.delegate = delegate;
}
public RingBankAsyncTask(Context context, HashMap<String, String> postData, boolean showLoadingMessage, AsyncResponse delegate) {
this.context = context;
this.postData = postData;
this.delegate = delegate;
this.showLoadingMessage = showLoadingMessage;
}
public RingBankAsyncTask(Context context, String loadingMessage, AsyncResponse delegate) {
this.context = context;
this.loadingMessage = loadingMessage;
this.delegate = delegate;
}
public RingBankAsyncTask(Context context, HashMap<String, String> postData, String loadingMessage, AsyncResponse delegate) {
this.context = context;
this.postData = postData;
this.loadingMessage = loadingMessage;
this.delegate = delegate;
}
protected void onPreExecute() {
if (this.showLoadingMessage == true) {
this.progressDialog = new ProgressDialog(this.context);
this.progressDialog.setMessage(this.loadingMessage);
this.progressDialog.show();
}
super.onPreExecute();
}
protected String doInBackground(String[] urls) {
String result = "";
for (int i = 0; i <= 0; i++) {
result = invokePost(urls[i], this.postData);
}
return result;
}
private String invokePost(String requestURL, HashMap<String, String> postDataParams) {
String response = "";
try {
URL url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = br.readLine()) != null)
response = new StringBuilder().append(response).append(line).toString();
}
else {
response = "";
Log.i("RingBankAsyncTask", new StringBuilder().append(responseCode).append("").toString());
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry entry : params.entrySet()) {
if (first)
first = false;
else {
result.append("&");
}
result.append(URLEncoder.encode((String)entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode((String)entry.getValue(), "UTF-8"));
}
return result.toString();
}
protected void onPostExecute(String result) {
if ((this.showLoadingMessage == true) &&
(this.progressDialog.isShowing())) {
this.progressDialog.dismiss();
}
result = result.trim();
this.delegate.processFinish(result);
}
public String getLoadingMessage() {
return this.loadingMessage;
}
public void setLoadingMessage(String loadingMessage) {
this.loadingMessage = loadingMessage;
}
public HashMap<String, String> getPostData() {
return this.postData;
}
public void setPostData(HashMap<String, String> postData) {
this.postData = postData;
}
public Context getContext() {
return this.context;
}
public AsyncResponse getDelegate() {
return this.delegate;
}
}
我這樣稱呼它:
RingBankAsyncTask task = new RingBankAsyncTask(getActivity(), this);
task.execute(myUrl);
要每10秒顯示一次數據還是要先顯示數據? –
我希望在我的Fragment可見時顯示數據,並且我不希望每次執行我的任務時我的Fragment重新啓動 – Mehran
結果是否表示您的StringBuilder或此代碼的結果? – SanVed