Android IHTTPSession parms数据保存到SQLite

时间:2016-08-30 05:15:16

标签: android sqlite android-studio

我想将来自Web浏览器的数据保存到SQLite数据库

这是我的代码

WebServer.java

public class WebServer extends NanoHTTPD {

public WebServer(int port) {
    super(port);
}

    public Response serve(IHTTPSession session) {
    String msg = "<html><body><h1>SERVER</h1>\n";
    Map<String, String> parms = session.getParms();
    if (parms.get("item_name") == null && parms.get("quantity") == null && parms.get("instruction") == null && parms.get("pos_id") == null) {
        msg += "<form action='?' method='get'>\n  " +
                "<p>ITEM NAME : <input type='text' name='item_name'></p>\n" +
                "<p>QUANTITY : <input type='text' name='quantity'></p>\n" +
                "<p>INSTRUCTION : <input type='text' name='instruction'></p>\n" +
                "<p>POS ID : <input type='text' name='pos_id'></p>\n" +
                "<p> <input type='submit'> </p> \n" +
                "</form>\n";
    } else {
        msg += "<p>ITEM NAME :, " + parms.get("item_name") + "!</p>\n";
        msg += "<p>QUANTITY :, " + parms.get("quantity") + "!</p>\n";
        msg += "<p>INSTRUCTION :, " + parms.get("instruction") + "!</p>\n";
        msg += "<p>POS ID :, " + parms.get("pos_id") + "!</p>\n";
    }
    return newFixedLengthResponse( msg + "</body></html>\n" );
  }

}

我已经创建了数据库帮助程序,将创建数据库

   public class DatabaseHelper extends SQLiteOpenHelper {
   public static final String DATABASE_NAME = "TableOrder.db";
   public static final String TABLE_NAME = "Table_order";
   public static final int DB_VERSION = 1;
   public static final String COL_1 = "ID";
   public static final String COL_2 = "Item_Name";
   public static final String COL_3 = "Quantity";
   public static final String COL_4 = "Instruction";
   public static final String COL_5 = "POS_ID";

   public DatabaseHelper(Context context) {

      super(context, DATABASE_NAME, null, DB_VERSION);
      SQLiteDatabase db = this.getWritableDatabase();
  }


   @Override
   public void onCreate(SQLiteDatabase db) {

      db.execSQL("create table " + TABLE_NAME + "ID INTEGER PRIMARY KEY AUTOINCREMENT, Item_Name TEXT," +
            "Quantity INT, Instruction TEXT, POS_ID INT");
   }

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

      db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
      onCreate(db);

   }



   }

我希望在单击提交按钮后,WebServer.java上的html代码的“名称”将保存到SQLite上

非常感谢任何帮助

谢谢

1 个答案:

答案 0 :(得分:1)

你可以使用Jsoup lib进行html解析,你可以获取html文件的元素和标签,并且插入你可以使用简单的sqlite和android插入方法。

对于基本示例,您可以按照这样的教程

http://www.survivingwithandroid.com/2014/04/parsing-html-in-android-with-jsoup-2.html

对于您的输入类型值,您可以使用

  

Element.select(&#34;输入[名称= ITEM_NAME]&#34)。ATTR(&#34;值&#34);   Element.select(&#34;输入[名称=量]&#34)。ATTR(&#34;值&#34);   Element.select(&#34;输入[名称=指令]&#34)ATTR(&#34;值&#34);   Element.select(&#34;输入[名称= pos_id]&#34)。ATTR(&#34;值&#34);