2013-03-29 77 views
1

我嘗試將html頁面存儲爲db作爲string.then我需要在webview中顯示 頁面。在這裏我粘貼我的代碼請幫助我....... 如何在Android中的SQLite數據庫中存儲HTML頁面?我試過 將html頁面轉換成字節數組存儲在數據庫中。請 幫助我如何插入存儲到數據庫,然後在 的WebView打開負載...如何從數據庫中顯示html頁面

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

    } 

請幫助我.....

+0

你會得到什麼錯誤?沒有?要麼? – BerggreenDK

+0

它不會顯示HTML頁面 –

+0

所以頁面是「空白」? – BerggreenDK

回答

0

你嘗試過這個例子:

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。

請讓我知道這是否有效,並且是答案。

相關問題