如何在我的PortParser类中创建上下文

时间:2019-04-09 15:59:32

标签: android database android-asynctask

我有一个实现AsyncTask的类。该类下载端口csv文件,并将其逐行导入mysqlite表。创建数据库的一部分是传递当前上下文。我在创建上下文时遇到问题。通常getApplicationContext()为我做这件事。但在这种情况下不起作用。我怎样才能做到这一点? (课程尚未完全结束)另一件事是,我发现android studio填补了我课程的完整路径。在创建文件夹并开始组织文件夹中的类之后,这种情况开始发生。我正在导入org.pctechtips.netdroid。*;但这不起作用。

package org.pctechtips.netdroid;

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

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";
    org.pctechtips.netdroid.dbhelper.DatabaseHelper dbHelper;
    Context context;



    public class DownloadPortFile extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            context = (Context) org.pctechtips.netdroid.activity.MainActivity;
            dbHelper = new org.pctechtips.netdroid.dbhelper.DatabaseHelper(context);
        }

        @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();
                if (connection.getResponseCode() != HttpsURLConnection.HTTP_OK) {
                   // publishProgress;
                   //reuturn
                }

                in = new BufferedReader(new InputStreamReader(new GZIPInputStream(connection.getInputStream()), "UTF-8"));
                String line;

                while ((line = in.readLine()) != null) {
                    if (isCancelled()) {
                        return;
                    }

                    /*String[] data = parser.parseLine(line);
                    if (data == null) {
                        continue;
                    }

                    if (parser.saveLine(db, data) == -1) {
                        publishProgress("Failed to insert data into the database. Please run this operation again");

                        return;
                    }*/
                }

            } catch (Exception e) {
//                publishProgress(e.toString());
            } 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 :(得分:0)

您需要将Context作为参数传递给此类的构造函数,并将其保存在那里。传递应用程序上下文。而且我建议您研究类与实例之间的区别,这里的代码表明您完全不了解Java及其对象模型。