我有一个PortParser类,该类下载文件并解析前4列并插入到表中。该类将DatabaseHelper类实例化为dbHelper。我发现,在while循环内的doInBackground()方法中,语句
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() {
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();
if (connection.getResponseCode() != HttpsURLConnection.HTTP_OK) {
Log.v("ERROR", "CONNECTION ERROR");
}
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 {
}
return null;
}
@Override
protected void onProgressUpdate(Void... voids) {
}
@Override
protected void onPostExecute(Void aVoid) {
}
}
}
在第二行导致异常,因此退出while循环。我该如何解决?我的dbHelper实例是否为空?
package org.pctechtips.netdroid.dbhelper;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
/**
* Created by jlvaz on 4/12/2017.
* Database Helper file
*/
public class DatabaseHelper extends SQLiteOpenHelper {
//The Android's default system path of your application database.
private static String DB_PATH = "";
private static String DB_NAME = "netdb.db";
private static String TABLE_NAME = "ports";
private static int DB_VERSION = 1;
/*SQL Query to get service running on especific port*/
private static String SQL_QUERY = "SELECT service FROM ports WHERE port==";
private static String SQL_CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (service TEXT, port INT(11), protocol TEXT, description TEXT);";
private SQLiteDatabase mDataBase;
private final Context context;
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources. *
* @param context
*/
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
DB_PATH = context.getDatabasePath(DB_NAME).getPath();
this.context = context;
}
public void createDataBase() throws IOException {
if(!checkDataBase()) {
this.getReadableDatabase();
copyDataBase();
this.close();
}
}
private boolean checkDataBase() {
File DbFile = new File(DB_PATH + DB_NAME);
return DbFile.exists();
}
boolean openDataBase() throws SQLException {
mDataBase = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.CREATE_IF_NECESSARY);
return mDataBase != null;
}
public synchronized void close(){
if(mDataBase != null)
mDataBase.close();
SQLiteDatabase.releaseMemory();
super.close();
}
/**
* create port table in netdb
* @param db
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_TABLE);
}
/**
* add record to port table on netdb
* @param service
* @param port
* @param proto
* @param desc
* @return
*/
public long addTableRecord(String service, int port, String proto, String desc) {
ContentValues value = new ContentValues();
value.put("service", service);
value.put("port", port);
value.put("protocol", proto);
value.put("description", desc);
return mDataBase.insert(TABLE_NAME, null, value);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
/**
* copy database to app location
* @throws IOException
*/
private void copyDataBase() throws IOException {
InputStream mInput = context.getAssets().open(DB_NAME);
String outfileName = DB_PATH;
OutputStream mOutput = new FileOutputStream(outfileName);
byte[] buffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(buffer))>0) {
mOutput.write(buffer, 0, mLength);
}
mOutput.flush();
mInput.close();
mOutput.close();
}
/*
* SQL query to get servive description
* of especific port
* */
public String getPortService(int port) {
String service = "";
SQLiteDatabase db = this.getReadableDatabase();
String sqlQuery = SQL_QUERY + port + ";";
Cursor cursor = db.rawQuery(sqlQuery,null);
if (cursor.getCount() > 0) {
cursor.moveToFirst();
service = cursor.getString(0);
}
// make sure to close the cursor
cursor.close();
return (service != "")? service : "Unknown!";
}
}
DatabaseHelper类
Git failed with a fatal error.
error: open("FinalWorkFinder/FinalWorkFinder/App_Data/aspnet-FinalWorkFinder-20190303122718.mdf"): Permission denied
fatal: Unable to process path FinalWorkFinder/FinalWorkFinder/App_Data/aspnet-FinalWorkFinder-20190303122718.mdf