我正在阅读格式为
的网址name \n
bio \n
URL link to image \n
name \n
bio \n
...
MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.URL_edit_text);
editText.setText("http:...txt");
db = new DatabaseHelper(this, null, null, 1);
}
public void To_URL(View view){
Button button = (Button) findViewById(R.id.URL_Button);
final String url = "http:....txt";
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, DisplayActivity.class);
try {
URL url = new URL("http:...txt");
intent.putExtra(url.toString(), "url_string");
Log.e(TAG,url.toString());
startActivity(intent);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
});
}
DisplayActivity
public class DisplayActivity extends AppCompatActivity {
DatabaseHelper db;
String url;
String name, bio;
TextView name_text_view, bio_text_view;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.db_layout);
name_text_view = (TextView) findViewById(R.id.name);
bio_text_view = (TextView) findViewById(R.id.bio);
url = getIntent().getStringExtra("url_string");
try {
InputStream source = new URL(url).openStream();
Scanner scanner = new Scanner(source).useDelimiter("\\n");
name = scanner.next();
bio = scanner.next();
// name = new Scanner(new URL(url).openStream(), "UTF-8").useDelimiter("\\n").next();
} catch (IOException e) {
e.printStackTrace();
}
db = new DatabaseHelper(this, null, null, 1);
printDatabase();
}
public void searchButtonClick(View view) {
}
public void populateButtonClick(View view) {
Person person = new Person(name, name);
db.addPerson(person);
}
public void clearButtonClick(View view) {
}
public void printDatabase(){
String dbStringName = db.dbToNameString();
String dbStringBio = db.dbToBioString();
name_text_view.setText(dbStringName);
bio_text_view.setText(dbStringBio);
}
}
DatabaseHelper
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="bio.db";
private static final int SCHEMA=1;
static final String COLUMN_NAME="name";
static final String COLUMN_BIOGRAPHY="bio";
static final String COLUMN_ID = "_id";
static final String TABLE_PERSONS="persons";
static final String PIC_URL="url";
String url_val;
public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, null, SCHEMA);
}
public void parseURL(String url){
url_val = url;
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_PERSONS + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME + " TEXT, " + COLUMN_BIOGRAPHY + " TEXT " + ");";
db.execSQL(query);
/*
cv.put(TITLE, "Gravity, Death Star I");
cv.put(VALUE, SensorManager.GRAVITY_DEATH_STAR_I);
db.insert(TABLE, TITLE, cv);
*/
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PERSONS);
onCreate(db);
}
public void addPerson(Person person){
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, person.get_name());
values.put(COLUMN_BIOGRAPHY, person.get_biography());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_PERSONS, null, values);
db.close();
}
public void deletePerson(String personName){
SQLiteDatabase db= getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_PERSONS + " WHERE " + COLUMN_BIOGRAPHY + "=\"" + personName + "\";");
}
//Print out database as String
public String dbToNameString(){
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_PERSONS + " WHERE 1";
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while(!c.isAfterLast()){
if(c.getString(c.getColumnIndex("name"))!= null){
dbString += c.getString(c.getColumnIndex("name"));
dbString += "\n";
}
}
c.close();
db.close();
return dbString;
}
//Print out database as String
public String dbToBioString(){
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_PERSONS + " WHERE 1";
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while(!c.isAfterLast()){
if(c.getString(c.getColumnIndex("bio"))!= null){
dbString += c.getString(c.getColumnIndex("bio"));
dbString += "\n";
}
}
c.close();
db.close();
return dbString;
}
}
现在我收到此错误消息 java.net.MalformedURLException
我不知道我是否存储了我的名字和生物在SQLite中正确。 如何正确自动存储名称和生物字段进入SQLite数据库,直到读取整个URL?
答案 0 :(得分:0)
在DisplayActivity
中,您尝试使用DatabaseHelper
变量db
,而不是在printDatabase()
方法中初始化它,该方法为空。
DatabaseHelper db;
public void printDatabase(){
String dbStringName = db.dbToNameString();
String dbStringBio = db.dbToBioString();
name_text_view.setText(dbStringName);
bio_text_view.setText(dbStringBio);
}
您需要像使用MainActivity
之前一样对其进行初始化。使用前
db = new DatabaseHelper(this, null, null, 1);