作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是java和android的新手。我想在 asyntask 类中获取 sqlite 行。但它返回 NullpointerException
我不知道如何传递 key ,任何人都可以告诉我实现这一目标的正确方法是什么。
类数据库
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME="cloud_contacts";
public static final String TABLE_LOGIN="login";
public static final String KEY_ID="id";
private static final String KEY_USERNAME="uname";
private static final String KEY_PASSWORD="password";
public static final String KEY_USERNAME_DEVICE="unamedevice";
public static final String KEY_PASSWORD_DEVICE="passworddevice";
private static final String CREATE_LOGIN_TABLE="CREATE TABLE "+TABLE_LOGIN + " ("
+KEY_ID+" INTEGER PRIMARY KEY, "
+KEY_USERNAME + " TEXT, "
+KEY_PASSWORD + " TEXT, "
+KEY_USERNAME_DEVICE + " TEXT, "
+KEY_PASSWORD_DEVICE+" TEXT"+ ")";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
/**
* tao bang sql user
*
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_LOGIN_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);
onCreate(db);
}
public String getKeyUsernameDevice(int id) {
String infor = null;
SQLiteDatabase db=DatabaseHandler.this.getWritableDatabase();
Cursor cursor = db.rawQuery("select " + KEY_USERNAME_DEVICE + " from " + TABLE_LOGIN + " where " + KEY_ID
+ " =? ", new String[]{String.valueOf(1)});
if (cursor != null) {
// move cursor to first row
if (cursor.moveToFirst()) {
do {
// Get version from Cursor
infor = cursor.getString(cursor.getColumnIndex(KEY_USERNAME_DEVICE));
// add the bookName into the bookTitles ArrayList
// move to next row
} while (cursor.moveToNext());
}
}
return infor;
}
public String getKeyPasswordDevice(int id) {
String infor = null;
SQLiteDatabase db=DatabaseHandler.this.getWritableDatabase();
Cursor cursor = db.rawQuery("select " + KEY_PASSWORD_DEVICE + " from " + TABLE_LOGIN + " where " + KEY_ID
+ " =? ", new String[]{String.valueOf(1)});
if (cursor != null) {
// move cursor to first row
if (cursor.moveToFirst()) {
do {
// Get version from Cursor
infor = cursor.getString(cursor.getColumnIndex(KEY_PASSWORD_DEVICE));
// add the bookName into the bookTitles ArrayList
// move to next row
} while (cursor.moveToNext());
}
}
return infor;
}
public class MyCommandTask extends AsyncTask<String,Void,Document>
{
String username;
String password;
DatabaseHandler databaseHandler;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Document doInBackground(String... params) {
InputStream inputStream = null;
HttpURLConnection urlConnection = null;
try {
this.databaseHandler=new DatabaseHandler(getApplicationContext());
username=databaseHandler.getKeyUsernameDevice(1);
password="admin";
/* forming th java.net.URL object */
URL url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
String basicAuth ="Basic " + Base64.encodeToString((username + ":" + password).getBytes(),Base64.NO_WRAP);
urlConnection.setRequestProperty("Authorization", basicAuth);
/* for Get request */
urlConnection.setRequestMethod("GET");
inputStream =urlConnection.getInputStream();
/* 200 represents HTTP OK */
Document responseXML = parseXmlDom(inputStream);
return responseXML;
}
catch (Exception ex)
{
ex.printStackTrace();
}
return null;
}
最佳答案
好吧,首先要提高性能,不要在游标的所有迭代中搜索相同的 columnIndex。
你应该这样做:
if (cursor != null) {
Integer colIndex = cursor.getColumnIndex(KEY_USERNAME_DEVICE);
// move cursor to first row
if (cursor.moveToFirst()) {
do {
// Get version from Cursor
infor = cursor.getString( colIndex );
// add the bookName into the bookTitles ArrayList
// move to next row
} while (cursor.moveToNext());
}
}
NullpointerException
的位置吗?是?
关于java - android 如何在android的Asynctask中加载sqlite数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34804115/
我是一名优秀的程序员,十分优秀!