在Android中执行一次异步任务

Posted

技术标签:

【中文标题】在Android中执行一次异步任务【英文标题】:executing Async task in Android once 【发布时间】:2017-08-21 01:43:12 【问题描述】:

嗨,我在这里需要帮助onCreate 方法

我已经实现了这一点,并将数据保存在 onSaveInstanceState​ 但它不断下载设备旋转上的数据

这是我的 MainActivity 代码

package com.ksamj.top10downloader;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity 

    private static final String TAG = "MainActivity";
    private ListView listApps;
    private String feedURL = "http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/limit=%d/xml";
    private int feedLimit = 10;
    private String feedCachedURL = "INVALID";
    public static final String STATE_URL = "feedURL";
    public static final String STATE_LIMIT = "feedLimit";

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listApps = (ListView) findViewById(R.id.xmlListView);


        if(savedInstanceState != null) // if null means run at 1st time
        

            feedURL = savedInstanceState.getString(STATE_URL);
            feedLimit = savedInstanceState.getInt(STATE_LIMIT);
            Log.d(TAG, "from Bundle feedURL: "+ feedURL);
            Log.d(TAG, "from Bundle feedLimit: "+ feedLimit);
        
        downloadURL(String.format(feedURL,feedLimit));
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        getMenuInflater().inflate(R.menu.feed_menu, menu);
        return true;
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        switch (item.getItemId()) 
            case R.id.menuFreeApps:
                feedURL = "http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/limit=%d/xml";
                break;
            case R.id.menuPaidApps:
                feedURL = "http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/toppaidapplications/limit=%d/xml";
                break;
            case R.id.menuSongs:
                feedURL = "http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=%d/xml";
                break;

            case R.id.menu10:
                feedLimit = 10;
                item.setChecked(true);
                break;

            case R.id.menu25:
                feedLimit = 25;
                item.setChecked(true);
                break;


            case R.id.menuRefresh:
                feedCachedURL = "INVALID";
                break;

            default:
                return super.onOptionsItemSelected(item);
        
        downloadURL(String.format(feedURL,feedLimit));
        return true;
    

    @Override
    protected void onSaveInstanceState(Bundle outState) 
        outState.putString(STATE_URL, feedURL);
        outState.putInt(STATE_LIMIT, feedLimit);
        super.onSaveInstanceState(outState);
    



    private void downloadURL(String feedURL) 
        if (!feedURL.equalsIgnoreCase(feedCachedURL)) 
            Log.d(TAG, "downloadURL: Downloading...");
            DownloadData downloadData = new DownloadData();
            downloadData.execute(feedURL);
            feedCachedURL = feedURL;
            Log.d(TAG, "downloadURL: feedURL " + feedURL);
            Log.d(TAG, "downloadURL: feedCachedURL " + feedCachedURL);
        else
            Log.d(TAG, "downloadURL: Nothing to Download");
    

    private class DownloadData extends AsyncTask<String, Void, String> 
        private static final String TAG = "DownloadData";

        @Override
        protected void onPostExecute(String s) 
            super.onPostExecute(s);
            //Log.d(TAG, "onPostExecute: params are" + s);
            ParseApplications parseApplications = new ParseApplications();
            parseApplications.parse(s);

            FeedAdapter feedAdapter = new FeedAdapter(MainActivity.this, R.layout.list_record, parseApplications.getApplications());
            listApps.setAdapter(feedAdapter);
        

        @Override
        protected String doInBackground(String... strings) 
            Log.d(TAG, "doInBackground: starts with " + strings[0]);
            String rssFeed = downloadXML(strings[0]);
            if (rssFeed == null) 
                Log.e(TAG, "doInBackground: Error Downloading");
            

            return rssFeed;
        

        private String downloadXML(String urlPath) 
            StringBuilder xmlResult = new StringBuilder();
            try 
                URL url = new URL(urlPath);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                int response = httpURLConnection.getResponseCode();
                Log.d(TAG, "downloadXML: The response Code is " + response);
                BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));

                int charsRead;
                char[] inputBuffer = new char[5500];
                while (true) 
                    charsRead = reader.read(inputBuffer);
                    if (charsRead < 0)
                        break;
                    if (charsRead > 0)
                        xmlResult.append(String.copyValueOf(inputBuffer, 0, charsRead));
                
                reader.close();

                return xmlResult.toString();

             catch (MalformedURLException ex) 
                Log.e(TAG, "downloadXML: Malformated URL Exp: " + ex.getMessage());

             catch (IOException ex) 
                Log.e(TAG, "downloadXML: IO Exp: " + ex.getMessage());
            

            return null;
        

    


这是我的 logcat,用于执行后的简单轮换

03-28 10:01:56.373 21047-21047/com.ksamj.top10downloader W/art: Verification of java.lang.Object com.ksamj.top10downloader.MainActivity.access$super(com.ksamj.top10downloader.MainActivity, java.lang.String, java.lang.Object[]) took 100.789ms
03-28 10:01:56.515 21047-21047/com.ksamj.top10downloader W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
03-28 10:01:56.805 21047-21047/com.ksamj.top10downloader D/MainActivity: downloadURL: Downloading...
03-28 10:01:56.809 21047-21047/com.ksamj.top10downloader D/MainActivity: downloadURL: feedURL http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/limit=10/xml
03-28 10:01:56.809 21047-21047/com.ksamj.top10downloader D/MainActivity: downloadURL: feedCachedURL http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/limit=10/xml
03-28 10:01:56.809 21047-21272/com.ksamj.top10downloader D/DownloadData: doInBackground: starts with http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/limit=10/xml
03-28 10:01:56.813 21047-21272/com.ksamj.top10downloader D/NetworkSecurityConfig: No Network Security Config specified, using platform default
03-28 10:01:56.953 21047-21274/com.ksamj.top10downloader I/OpenGLRenderer: Initialized EGL, version 1.4
03-28 10:01:56.953 21047-21274/com.ksamj.top10downloader D/OpenGLRenderer: Swap behavior 1
03-28 10:01:56.957 21047-21274/com.ksamj.top10downloader W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
03-28 10:01:56.957 21047-21274/com.ksamj.top10downloader D/OpenGLRenderer: Swap behavior 0
03-28 10:01:57.002 21047-21272/com.ksamj.top10downloader D/DownloadData: downloadXML: The response Code is 200
03-28 10:01:57.098 21047-21047/com.ksamj.top10downloader W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
03-28 10:01:57.403 21047-21047/com.ksamj.top10downloader W/InputMethodManager: Ignoring onBind: cur seq=94, given seq=93
03-28 10:02:02.331 21047-21047/com.ksamj.top10downloader D/MainActivity: from Bundle feedURL: http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/limit=%d/xml
03-28 10:02:02.332 21047-21047/com.ksamj.top10downloader D/MainActivity: from Bundle feedLimit: 10
03-28 10:02:02.332 21047-21047/com.ksamj.top10downloader D/MainActivity: downloadURL: Downloading...
03-28 10:02:02.333 21047-21047/com.ksamj.top10downloader D/MainActivity: downloadURL: feedURL http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/limit=10/xml
03-28 10:02:02.333 21047-21047/com.ksamj.top10downloader D/MainActivity: downloadURL: feedCachedURL http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/limit=10/xml
03-28 10:02:02.336 21047-21345/com.ksamj.top10downloader D/DownloadData: doInBackground: starts with http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/limit=10/xml
03-28 10:02:02.354 21047-21345/com.ksamj.top10downloader D/DownloadData: downloadXML: The response Code is 200

你能帮忙找出我做错了什么或遗漏了什么吗?

【问题讨论】:

您的应用程序支持两个方向?如果是,那么您需要在保存的实例中管理方向更改保存数据。 【参考方案1】:

使用加载器模式。加载器允许你注册一个异步进程,这样你就不会启动额外的任务,所以当它完成时存在的 Activity 会得到实际的结果。详情请见https://developer.android.com/reference/android/app/LoaderManager.html

【讨论】:

【参考方案2】:

每次旋转设备时feedCachedURL变量设置为INVALID,你也应该在旋转前保存它

【讨论】:

【参考方案3】:

当 savedInstance 为 null 时调用 downloadUrl 函数。 定义一个变量 rssFeed,您将在异步任务的 POstExecute 上获得该变量。

private String mRssFeed;

if(savedInstanceState != null) // if null means run at 1st time
    

        feedURL = savedInstanceState.getString(STATE_URL);
        feedLimit = savedInstanceState.getInt(STATE_LIMIT);
        Log.d(TAG, "from Bundle feedURL: "+ feedURL);
        Log.d(TAG, "from Bundle feedLimit: "+ feedLimit);
        mRssFeed = savedInstanceState.getString("RSS_FEED");
        if(mRssFeed != null && !mRssFeed.equals("")
          ParseApplications parseApplications = new ParseApplications();
          parseApplications.parse(mRssFeed);
          FeedAdapter feedAdapter = new FeedAdapter(MainActivity.this, R.layout.list_record, parseApplications.getApplications());
          listApps.setAdapter(feedAdapter);
        else
          downloadURL(String.format(feedURL,feedLimit));
        

    else
        downloadURL(String.format(feedURL,feedLimit));
    

您还需要在 OnSavedInstanceMethod 中保存您获得的提要,并在 postExecute 中设置 mRssFeed。

 @Override
protected void onSaveInstanceState(Bundle outState) 
    outState.putString(STATE_URL, feedURL);
    outState.putInt(STATE_LIMIT, feedLimit);
    //save the rssFeed here
    outState.putString("RSS_FEED", mRssFeed);
    super.onSaveInstanceState(outState);



 private class DownloadData extends AsyncTask<String, Void, String> 
    private static final String TAG = "DownloadData";

    @Override
    protected void onPostExecute(String s) 
        super.onPostExecute(s);
        //Log.d(TAG, "onPostExecute: params are" + s);
        mRssFeed = s;
        ParseApplications parseApplications = new ParseApplications();
        parseApplications.parse(mRssFeed);

        FeedAdapter feedAdapter = new FeedAdapter(MainActivity.this, R.layout.list_record, parseApplications.getApplications());
        listApps.setAdapter(feedAdapter);
    

    @Override
    protected String doInBackground(String... strings) 
        Log.d(TAG, "doInBackground: starts with " + strings[0]);
        String rssFeed = downloadXML(strings[0]);
        if (rssFeed == null) 
            Log.e(TAG, "doInBackground: Error Downloading");
        

        return rssFeed;
    

    private String downloadXML(String urlPath) 
        StringBuilder xmlResult = new StringBuilder();
        try 
            URL url = new URL(urlPath);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            int response = httpURLConnection.getResponseCode();
            Log.d(TAG, "downloadXML: The response Code is " + response);
            BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));

            int charsRead;
            char[] inputBuffer = new char[5500];
            while (true) 
                charsRead = reader.read(inputBuffer);
                if (charsRead < 0)
                    break;
                if (charsRead > 0)
                    xmlResult.append(String.copyValueOf(inputBuffer, 0, charsRead));
            
            reader.close();

            return xmlResult.toString();

         catch (MalformedURLException ex) 
            Log.e(TAG, "downloadXML: Malformated URL Exp: " + ex.getMessage());

         catch (IOException ex) 
            Log.e(TAG, "downloadXML: IO Exp: " + ex.getMessage());
        

        return null;
    


即使您正在从 savedInstance 恢复数据,您的 downloadUrl 也会被调用。将其放入 else 将确保不会调用它。

但正如 Gabe 所说,如果您正在下载并且状态发生变化,则可能会出现一些问题。在这种情况下,我建议您进一步使用 RxAndroid。与 AsyncTasks 相比,这为您提供了很多控制权。

以下是有关 android 中一些最佳异步编程实践的好读物:

http://code.hootsuite.com/asynchronous-android-programming-the-good-the-bad-and-the-ugly/

【讨论】:

这个解决方案的问题是只有第一个会运行。第一个是原始 Activity 的一部分,因此它在 onPostExecute 中所做的任何 UI 更改都将转到原始 Activity,而不是当前 Activity。 对不起,我没有明白你的意思。但是如果我们检查 rssFeed 是否为空,然后触发 downLoadUrl 方法,它应该可以正常工作。问题是如果应用程序已经有数据,那么不需要在状态变化时重新下载。如果没有数据,它将下载。你的想法? 这只是问题的一半。其他大厅是什么,如果它正在下载。然后,您可能会进行六次浪费的下载。由于同步任务共享同一个线程,那些无用的下载将在最后一个影响正确活动的下载之前发生。停止那是重要的事情。装载机就是解决这个问题的答案 是的,我同意 AsyncTask 在这方面很痛苦。这就是为什么而不是使用 Loader RxAndroid 是要走的路。它使我们对这些事情有很大的控制权。在这种情况下,我更喜欢使用 Rx。但既然问题是关于 AsyncTask 的,那么这就是它的答案。 Rx 为您提供了对状态更改的大量控制,因此是完成后台进程的最佳方式之一。不过 loader 也是一个不错的选择。 Rx android 可能是最常用的库。并非一切都是钉子。没有理由添加所有这些开销并最终得到 rx android 为此提供的更难调试的代码库。

以上是关于在Android中执行一次异步任务的主要内容,如果未能解决你的问题,请参考以下文章

Android-AnsyncTask异步任务

Android - 执行异步任务的最佳方式

Android AsyncTask异步任务的使用

Android_AsyncTask异步任务机制

异步编程

Android AsyncTask内部线程池异步执行任务机制简要分析