调用execute方法后,AsyncTasck类将不会执行

时间:2019-04-11 13:29:52

标签: android android-asynctask

我有一个用于下载文件的类(PortParser)。并在doInBackground方法中设置调试器之后。我可以看到在调用execute之后,它跳到MainActivity的下一行,而不是进入doInBackground内部。这可能是什么。我可以看到程序进入了execute方法中,该方法又调用了AsyncTask execute方法。但它永远不会进入doInBackground方法内部。谢谢。

这是主要活动类内部的调用实例

portParser = new PortParser(this.getApplicationContext());
portParser.execute();

package org.pctechtips.netdroid.classes;

import android.os.AsyncTask;
import android.content.Context;
import android.util.*;
import org.pctechtips.netdroid.dbhelper.*;
import java.util.*;

import java.io.*;
import java.net.*;
import java.util.zip.*;

import javax.net.ssl.*;

/**
 * Java class to downloand and parse service-port csv file from iana.org
 */

public class PortParser {
    //public static final String PORT_URL = "https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.csv";
    public static final String PORT_URL = "http://pctechtips.org/apps/service-names-port-numbers.csv";
    org.pctechtips.netdroid.dbhelper.DatabaseHelper dbHelper;
    DownloadPortFile downloadFile;
    android.database.sqlite.SQLiteDatabase db;
    Context context;

    public PortParser(Context ctxt) {
        dbHelper = new org.pctechtips.netdroid.dbhelper.DatabaseHelper(ctxt);
        db = dbHelper.getWritableDatabase();
        downloadFile = new DownloadPortFile();
    }

    public void execute() {
        Log.v("DOWNLOADING", "DOWNLOADING PORT FILE");
        downloadFile.execute();
    }


    public class DownloadPortFile extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... voids) {
            BufferedReader in = null;
            HttpsURLConnection connection = null;

            try {
                URL url = new URL(PORT_URL);
                connection = (HttpsURLConnection) url.openConnection();
//                connection.setRequestProperty("Accept-Encoding", "gzip");
                connection.connect();
                Log.v("CONNECTION", "CONNECTION OK");
                if (connection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
                   Log.v("CONNECTION", "CONNECTION OK");
                }
                in = new BufferedReader(new InputStreamReader(new GZIPInputStream(connection.getInputStream()), "UTF-8"));
                String line;
                int lineNum = 0;
                while ((line = in.readLine()) != null) {
                    String[] data = line.split(",", -1);
                    Log.v("DATA", Arrays.toString(data) +" "+ lineNum);
                    if(data.length != 12) { continue; }
                    if(data == null) { continue; }
                    if(!data[2].equalsIgnoreCase("tcp")) { continue; }

                    String service = (data[0].equals(" ")) ? "null" : data[0];
                    int portNum = Integer.parseInt(data[1]);
                    String protocol = data[2];
                    String desc = data[3];
                    Log.v("PARSED", service +" "+ portNum +" "+ protocol +" "+ desc +" "+data.length);

                    long dbInsert = dbHelper.addTableRecord(service, portNum, protocol, service);
                }

            } catch (Exception e) {

            } finally {
                /*try {
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException ignored) {
                }

                if (connection != null) {
                    connection.disconnect();
                }*/
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Void... voids) {

        }

        @Override
        protected void onPostExecute(Void aVoid) {

        }
    }

}

1 个答案:

答案 0 :(得分:1)

  

调用执行后,我可以看到它跳到MainActivity的下一行,而不是进入doInBackground内部

这应该是这样,因为没有理由仅因为您生成了其他异步工作线程而中断了主线程流。这实际上与异步功能是相反的。如果要调试doInBackground(),则应在该方法的代码上的某个地方设置一个断点(+,如果只是断点不起作用,则可能需要调用Debug.waitOnDebugger())。