jeudi 14 janvier 2016

android How to load sqlite data in Asyntask in android

I'm new to java and android. I want to get sqlite rows in a asyntask class .but it return NullpointerException i don't know how to pass the key can anybody tell me what is the correct way to achieve this.

class database

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;
}

class contain Asyntask

   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;

    }

Aucun commentaire:

Enregistrer un commentaire