1

我尝试将 htmlpage 作为字符串存储在 db 中。然后我需要在 webview 中显示该页面。在这里我粘贴我的代码请帮助我.......你如何在android的sqlite数据库中存储一个html页面?我试图将 html 页面转换为字节数组以存储在数据库中。请帮助我如何将插入存储到数据库中,然后在 webview 中打开该负载...

public class AndroidOpenDbHelper extends SQLiteOpenHelper  {
    public static final String DB_NAME = "html_db";
    public static final int DB_VERSION = 1;
    public static final String TABLE_NAME = "html_table";
    public static final String COLUMN_NAME_ID = "html_id_column";
    public static final String COLUMN_NAME_PAGE = "page_column";


    public AndroidOpenDbHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        //"create table if not exists TABLE_NAME ( BaseColumns._ID integer primary key autoincrement, FIRST_COLUMN_NAME text not null, SECOND_COLUMN_NAME integer not null);"
                String sqlQueryToCreateTable = "create table if not exists " + TABLE_NAME + " ( " + BaseColumns._ID + " integer primary key autoincrement, " 
                                                                        + COLUMN_NAME_PAGE + " HTML STRING, "
                                                                        ;

                // Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
                db.execSQL(sqlQueryToCreateTable);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        if(oldVersion == 1 && newVersion == 2){
            // Upgrade the database
        }       
    }

    }

这是我显示页面的代码。

 public class MainActivity extends Activity {
     WebView browser;
    private static String inp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         browser=(WebView)findViewById(R.id.w1);




    }


     private void loadTime(SQLiteDatabase db) {
        // TODO Auto-generated method stub

         browser.getSettings().setJavaScriptEnabled(true);

                   browser.loadDataWithBaseURL("x-data://base",inp,"text/html", "UTF-8",null);

        // browser.loadUrl(inp);

    }
    public static String main(String args[]) throws FileNotFoundException {
         FileInputStream fis = new FileInputStream("D:/mo.html");
          inp = new Scanner(fis,"UTF-8").useDelimiter("\\A").next();
    return inp;
     }
    public void insertUndergraduate(){
        AndroidOpenDbHelper androidOpenDbHelperObj = new AndroidOpenDbHelper(this);
        SQLiteDatabase sqliteDatabase = androidOpenDbHelperObj.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(AndroidOpenDbHelper.COLUMN_NAME_PAGE,inp);

    }

请帮助我.....

4

1 回答 1

1

你试过这个例子吗:

http://mobile.tutsplus.com/tutorials/android/android-sdk-embed-a-webview-with-the-webkit-engine/

这看起来不错:

WebView engine = (WebView) findViewById(R.id.web_engine); 

String data = "<html>"  +  
          "<body><h1>Yay, Mobiletuts+!</h1></body>"  +  
          "</html>";  

engine.loadData(data, "text/html", "UTF-8");  

现在,如果可行,我只需将 html 从数据库中的普通文本字段中选择到字符串中。

请让我知道这是否有效并且是答案。

于 2013-03-29T15:49:20.310 回答