【问题标题】:Want to save webview data from android to sqlite想要将 webview 数据从 android 保存到 sqlite
【发布时间】:2016-06-07 13:28:10
【问题描述】:

我尝试使用 javascriptInterface 将数据从 webview 解析到文本视图,以便我可以将文本存储在 sqlite 中,但没有成功。我是android的新手所以请指导我如何实现这一目标

【问题讨论】:

  • 如果您包含代码,这里可能会有人帮助您修复代码。但我怀疑你找不到太多的志愿者来为你写所有东西

标签: android sqlite webview


【解决方案1】:

试试下面的代码

final Context myctxt = this; //your activity or application context
    class MyJavaScriptInterface
    {

        @JavascriptInterface
        @SuppressWarnings("unused")
        public void processHTML(String html)
        {
            // process the html as needed by the app
            DBHelpher dbhelpher=new DBHelpher(myctxt);
            dbhelpher.insertHtmlString(html);

        }
    }

    final WebView browser = (WebView)findViewById(R.id.browser);
    /* JavaScript must be enabled if you want it to work, obviously */
    browser.getSettings().setJavaScriptEnabled(true);

    /* Register a new JavaScript interface called HTMLOUT */
    browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");

    /* WebViewClient must be set BEFORE calling loadUrl! */
    browser.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url)
        {
            /* This call inject JavaScript into the page which just finished loading. */
            browser.loadUrl("javascript:window.HTMLOUT.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
        }
    });

    /* load a web page */
    browser.loadUrl("YOUR LINK");


    Create class for sqlite

    public class DBHelpher extends SQLiteOpenHelper 
    {

     Context context;
        public final String TABLE_SAMPLE="test";
          public final String COLUMN_ID="id";
        public final String COLUMN_HTMLSTRING="htmlstring";

        public final String CREATE_TABLE__SAMPLE_SQL="create table " + TABLE_SAMPLE
                + " (" + COLUMN_ID + " integer primary key autoincrement, "
                + COLUMN_HTMLSTRING + " text "
                + ");";


        public DBHelpher(Context context)
        {
            super(context, "DATABASENAME.db", null, 1);
            this.context=context;
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(CREATE_TABLE__SAMPLE_SQL);


        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }


        public void insertHtmlString(String htmlString)
        {

            SQLiteDatabase database = this.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(COLUMN_HTMLSTRING,htmlString);
             database.insert(TABLE_SAMPLE, null, values);


        }


    }

您将在 processHTML 方法中获得整个 Html 网络。并且它不会再次请求网页。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 2014-01-23
    • 1970-01-01
    • 2014-10-15
    相关资源
    最近更新 更多