[英]AsyncTasc… doInBacground repeats every time I change orientation of the screen
最近,我正在為網站開發android應用程序。 對於應用程序,我必須從服務器獲取數據。 現在,我正在使用AsyncTasc .... doInBackground
,以便在下載數據時在背景ans顯示器中獲取數據。
但是我有兩個問題:1.每次更改設備的方向時, doInBackground
都會重新開始,並且應用程序崩潰。 (我已將“ Activity
的方向設置為“ Portrait Mode
但這不是我想要的解決方案。
您能幫我如何改善此解決方案,甚至改用其他解決方案。
如果需要:我使用了下面的代碼:
private class GetNewsData extends
AsyncTask<String, Void, ArrayList<DashBoardModel>> {
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected ArrayList<DashBoardModel> doInBackground(String... URL) {
String categories_url = URL[0]; // Creating JSON Parser instance
JSONNumberParser jParser = new JSONNumberParser(); // getting JSON
// string from
// URL
JSONArray newsItems = jParser.getJSONFromUrl(categories_url);
Log.e("lsbsfbsfdbsfd", newsItems.toString());
try {
for (int i = 0; i < newsItems.length(); i++) {
JSONObject c = newsItems.getJSONObject(i);
.....more code over here....
}
} catch (JSONException e) {
e.printStackTrace();
}
.... more code over here...
return items;
}
@Override
protected void onPostExecute(ArrayList<DashBoardModel> items) {
customModelAdapter.notifyDataSetChanged();
}
}
下面是我通常使用的JSonParser.class
:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
JSONArray jArr = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONArray getJSONFromUrl(String url) {
Log.e("JSON Parser", "U futem tek Jason " );
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
Log.e("JSON Parser", "vajti " );
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Log.e("JSON Parser", json );
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
JSONTokener jt = new JSONTokener(json);
Object rootElement = jt.nextValue();
if (rootElement instanceof JSONObject) {
// You got an object from the jresponse
jObj = new JSONObject(json);
} else if (rootElement instanceof JSONArray) {
jArr = new JSONArray(json);
Log.e("JSON Parser", "erdhi" );
return jArr;
// You got a JSON array
}
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jArr;
}
}
這是活動的標准行為,您應該離開它。 為了避免重復下載,您可以做的是將下載的結果緩存在某個地方,例如在Singleton類中。.因此,下次運行AsyncTask時,它將檢查是否已經有“緩存的”數據,如果是,它將立即返回,否則它將執行下載。
通常,我們將此邏輯委托給另一個類,就像Singleton一樣,不受活動生命周期的影響。因此它不會受到影響。
這個想法是讓AsyncTask在單例上調用一個方法,例如:singleton.loadData(),然后它將在內部執行此邏輯檢查是否已緩存數據或是否需要下載數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.