Issue
I have a class to download a file (PortParser) class. and after setting the debugger inside doInBackground method. I can see after calling execute, it jumps to the next line in MainActivity instead of going inside doInBackground. what could this be. I can see the program going inside the execute method which in turn calls the AsyncTask execute method. but it never goes inside doInBackground method. Thanks.
this is the calling instance inside main activity class
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) {
}
}
}
Solution
I can see after calling execute, it jumps to the next line in MainActivity instead of going inside doInBackground
This is how it should be as there's no reason for main thread flow to be disrupted just because you spawned other asynchronous worker. That would be actually contrary to what async things are for. If you want to debug doInBackground()
you should set a breakpoint on that method's code somewhere (+ you may need to call Debug.waitOnDebugger() if just breakpoint won't work).
Answered By - Marcin Orlowski
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.