I have a class that implements AsyncTask. the class downloads port csv file and import line by line into a mysqlite table. part of creating the database is to pass the current context. and I'm having problem creating context. usually getApplicationContext() do it for me. But is not working in this case. How can I do this? (class is not fully finished) another thing is that I find android studio filling full path for my classes. this started happening after I created folder and started orginazing my classes inside folders. I'm importing org.pctechtips.netdroid.*; but that doesn't work.
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) {
}
}
}