我正在开发一个包括登录和注册过程的Android应用程序(Android Studio-Java)。我通过http实现PHP文件和MySQL数据库之间的连接,从而完成了登录和注册过程。简而言之,我刚刚在Java中创建了一个AsyncTask类(从另一个类中调用),并将其用于将数据发布到PHP文件,从那里我仅使用了适当的SQL命令。这部分工作正常。
登录和注册的第一部分很重要,因为每个用户登录后都会看到略有不同的布局。布局是由详细的CardView元素组成的RecyclerView。每个CardView都有一些带有一些详细信息的TextView。详细信息由我在单独的类中创建的对象保存。要填充CardView元素,请使用单独的PHP文件(以及另一个http连接)来获取一些JSON数据。将数据从JSON解析为Strings和int是一件很简单的事情,就像将它们添加到自定义对象列表一样。下面有一些代码显示我如何获取数据并将其添加到“对象”列表中。
这是完整的AsyncTask类:
public class ScheduleWorker extends AsyncTask<String, Void, String> {
Context context;
AlertDialog alertDialog;
ScheduleWorker (Context ctx) { context = ctx; }
@Override
protected void onPreExecute() { super.onPreExecute(); }
@Override
protected void onPostExecute (String s) {
super.onPostExecute(s);
Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
try {
loadJSON(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(String... params) {
final String fetch_url = "http://192.168.1.70/newfetcher.php";
try {
String ussr_name = params[0];
URL url = new URL(fetch_url);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
OutputStream outputStream = con.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("user_name", "UTF-8")+"="+URLEncoder.encode(ussr_name, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
private void loadJSON(String json) throws JSONException {
JSONArray jsonArray = new JSONArray(json);
Course course;
List<Course> courses = new ArrayList<Course>();
int len = jsonArray.length();
String[] titles = new String[len];
String[] types = new String[len];
String[] teachers = new String[len];
int[] pics = new int[len];
for (int i = 0; i < len; i++) {
JSONObject obj = jsonArray.getJSONObject(i);
titles[i] = obj.getString("title");
types[i] = obj.getString("type");
teachers[i] = obj.getString("teacher");
pics[i] = obj.getInt("pic");
course = new Course(titles[i], types[i], teachers[i], pics[i]);
courses.add(course);
}
}
}
现在,我遇到的问题是当我尝试将对象列表(及其详细信息)保存到Room Persistent Database时。我根据Google Developer CodeLabs的示例为会议室数据库创建了代码。我根据自己的特殊需求修改了代码,但保留了底层的类结构。该结构包括:实体,DAO,RoomDatabase,存储库,ViewModel,ViewHolder,RecyclerView的适配器以及用于填充数据库的类。除了我填充数据库的那一部分之外,其他一切似乎都很好。该示例通过在RoomDatabase类中使用回调和AsyncTask来填充数据库。
此处正在填充AsyncTask:
private static RoomDatabase.Callback sRoomDatabaseCallback = new RoomDatabase.Callback() {
@Override
public void onOpen(@NonNull SupportSQLiteDatabase db) {
super.onOpen(db);
// If you want to keep the data through app restarts,
// comment out the following line.
new PopulateDbAsync(INSTANCE).execute();
}
};
/**
* Populate the database in the background.
* If you want to start with more words, just add them.
*/
private static class PopulateDbAsync extends AsyncTask<Void, Void, Void> {
private final WordDao mDao;
PopulateDbAsync(WordRoomDatabase db) {
mDao = db.wordDao();
}
@Override
protected Void doInBackground(final Void... params) {
// Start the app with a clean database every time.
// Not needed if you only populate on creation.
Word word = new Word("Hello");
mDao.insert(word);
word = new Word("World");
mDao.insert(word);
return null;
}
}
我的问题是如何用详细信息数组和/或“对象”列表填充数据库?该示例在每次调用添加活动时执行回调。我只想在用户注册时填充数据库。