我正在尝试用一个按钮更新AndroidStudio中的应用数据库。当您按下它时,程序会从xampp下载URL为http://IP:Port/user/bdcortes.db的文件“ bdcortes.db”,并覆盖以下路径中的现有文件:/data/data/com.example.user.app/databases/
自从数据库文件出现最后一个问题以来,我已修复了xampp中的一些错误,如ip和相应URL的端口。但是现在我有另一个问题。当我处理微调器中的数据时,我的程序无法正确读取数据库(一旦我在模拟器中有了新的db文件)。我不知道在完成下载文件或向另一个 TestAdapter 收费后,我是否必须停止Thread,我在其中对数据库文件进行所有查询。
谢谢!
public class Notas extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner spinnerUni, spinnerGrad, spinnerAnio;
TextView textView;
Context myContext = this;
ProgressDialog dialog;
String fileDownloadPath = "http://localhost:4432/mario/bdcortes";
String fileSavePath = "/data/data/com.example.mariorg.app/databases/";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notas);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
TestAdapter mDbHelper = new TestAdapter(this);
mDbHelper.createDatabase();
mDbHelper.open();
dialog = null;
// Inicialización de los elementos layout
textView = findViewById(R.id.tvResultado);
spinnerUni = findViewById(R.id.spUniversidad);
spinnerGrad = findViewById(R.id.spGrado);
spinnerAnio = findViewById(R.id.spAnio);
spinnerUni.setOnItemSelectedListener(this);
spinnerGrad.setOnItemSelectedListener(this);
spinnerAnio.setOnItemSelectedListener(this);
loadAllSpinners();
spinnerUni.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
loadGrades(spinnerUni.getSelectedItem().toString(), spinnerAnio.getSelectedItem().toString());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public void actualizar(View view){
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
// dialog = ProgressDialog.show(Notas.this, "", "Actualizando...", true);
new Thread(new Runnable(){
@Override
public void run(){
downloadFile(fileDownloadPath, fileSavePath);
}
}).start();
break;
case DialogInterface.BUTTON_NEGATIVE:
Toast toast1 = Toast.makeText(getApplicationContext(), "ERROR AL DESCARGAR BD", Toast.LENGTH_SHORT);
toast1.show();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Esto descargará los años previos a 2018. ¿Está seguro?").setPositiveButton("Sí", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
public void downloadFile(String fileDownloadPath, String fileSavePath){
try{
File saveFile = new File(fileSavePath);
URL u = new URL(fileDownloadPath);
URLConnection con = u.openConnection();// abrir conexion
int lenghtofContent = con.getContentLength();
DataInputStream DIStream = new DataInputStream(u.openStream());
byte[] buffer = new byte[lenghtofContent];
DIStream.readFully(buffer);
DIStream.close();
Log.e("HOLA", DIStream.toString());
DataOutputStream DOStream = new DataOutputStream(new FileOutputStream(saveFile));
DOStream.write(buffer);
DOStream.flush();
DOStream.close();
Toast toast1 = Toast.makeText(Notas.this, "ACTUALIZADO", Toast.LENGTH_SHORT);
toast1.show();
//hideProgressIndicator();
}catch (FileNotFoundException e){
//hideProgressIndicator();
}catch (IOException e){
//hideProgressIndicator();
}catch (Exception e){
}
}
private void hideProgressIndicator() {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.dismiss();
}
});
}
我已经测试了应用程序可以正确下载数据库文件,但是当您按下一个微调器时,不会在微调器中收费数据。