insertDatabase
package com.example.rikao080902.util; import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import com.example.rikao080902.MyDatabaseHelper; /** * */ public class InsertDatabase { private final SQLiteDatabase sd; public InsertDatabase(Context context){ MyDatabaseHelper helper=new MyDatabaseHelper(context); sd = helper.getWritableDatabase(); } public boolean insert(String step){ ContentValues values=new ContentValues(); values.put("step",step); long user = sd.insert("users", null, values); if(user!=-1){ return true; }else{ return false; } } }MainActivity
package com.example.rikao080902; import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.example.rikao080902.bean.MenuInfo; import com.example.rikao080902.util.InsertDatabase; import com.example.rikao080902.util.StreamTools; import com.google.gson.Gson; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.List; public class MainActivity extends AppCompatActivity { private SQLiteDatabase db; private MyDatabaseHelper dbhelper; private Button button,button02; private List<MenuInfo.ResultBean.DataBean.StepsBean> list; private InsertDatabase insertDatabase; // private Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button= (Button) findViewById(R.id.button); button02= (Button) findViewById(R.id.button02); dbhelper=new MyDatabaseHelper(this); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dbhelper.getWritableDatabase(); Toast.makeText(MainActivity.this,"数据库创建完毕",Toast.LENGTH_SHORT).show(); } }); button02.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new AsyncTask<String, Void, String>() { @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson=new Gson(); MenuInfo menuInfo=gson.fromJson(s,MenuInfo.class); list=menuInfo.getResult().getData().get(0).getSteps(); insertDatabase=new InsertDatabase(MainActivity.this); for (int i=0;i<list.size();i++){ Boolean insert = insertDatabase.insert(list.get(i).getStep()); if(insert) { System.out.println("添加成功"); } } } @Override protected String doInBackground(String... params) { try { String path=params[0]; URL url=new URL(path); HttpURLConnection connection= (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); OutputStream os=connection.getOutputStream(); os.write("key=4866477692b8491f120e3608db277cb0&menu=红烧肉".getBytes()); os.flush(); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); int code=connection.getResponseCode(); if(code==HttpURLConnection.HTTP_OK){ InputStream is=connection.getInputStream(); String json= StreamTools.readNetWorks(is); return json; } } catch (Exception e) { e.printStackTrace(); } return null; } }.execute("http://apis.juhe.cn/cook/query.php"); } }); } }MyDatabaseHelper
package com.example.rikao080902; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; /** * */ public class MyDatabaseHelper extends SQLiteOpenHelper{ public MyDatabaseHelper(Context context) { super(context, "user.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table users(id integer primary key autoincrement,step varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }