Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android,下载并使用.db文件_Android_Mysql_Sqlite - Fatal编程技术网

Android,下载并使用.db文件

Android,下载并使用.db文件,android,mysql,sqlite,Android,Mysql,Sqlite,(我对我糟糕的英语深表歉意) 对于一个学校项目,我必须实现一个Android应用程序。它使用一个intern SQLite数据库,它是一个网站MySQL数据库的副本。(android应用程序是电气工程数据库的搜索引擎) 由于它必须独立于网站(离线),我必须创建一个更新选项 为此,我制作了一个特殊的DownloadHelper类: @SuppressLint("SdCardPath") public final class DownloadHelper extends AsyncTask<V

(我对我糟糕的英语深表歉意)

对于一个学校项目,我必须实现一个Android应用程序。它使用一个intern SQLite数据库,它是一个网站MySQL数据库的副本。(android应用程序是电气工程数据库的搜索引擎) 由于它必须独立于网站(离线),我必须创建一个更新选项

为此,我制作了一个特殊的DownloadHelper类:

@SuppressLint("SdCardPath")
public final class DownloadHelper extends AsyncTask<Void,Void,Void>
{
    Context context;
    File cheminBdd = new File("/data/data/com.example.btc_pe/databases/basesqlite.db");

    public DownloadHelper(Context ctxt)
      { this.context = ctxt; }

    @Override
    protected Void doInBackground(Void... params)
    {
        // TODO Auto-generated method stub
        try 
        {
            downloadDatabase(cheminBdd);
            //copyServerDatabase(this.context);
        } 
        catch (Exception ex) 
        {
            Log.e("BTC","Failed to download database !",ex);
        }

        return null;
    }


    private static void downloadDatabase(File destFile) throws IOException
    {        
        URLConnection ucon;
        InputStream is = null;
        OutputStream os = null;
        try 
        {
             Log.d("BTC","start DL");
             URL url = new URL("adresse" + "basesqlite.db");
             ucon = url.openConnection();
             Log.d("BTC","Connection open");
             is = ucon.getInputStream();
             Log.d("BTC","Stream In got");
             os = new FileOutputStream(destFile);
             Log.d("BTC","Debut copy()");
             copy(is,os);
             Log.d("BTC","end DL");
        } 
        finally 
        {
             if (os != null) try { os.close(); } catch (Exception ex) { Log.e("BTC","Failed to gracefully close output stream",ex); }
             if (is != null) try { is.close(); } catch (Exception ex) { Log.e("BTC","Failed to gracefully close input stream",ex); }
        } 
    }

    public static int copy(InputStream input, OutputStream output) throws IOException
    {
         byte[] buffer = new byte[8192];
         int count = 0;
         int n = 0;
         while (-1 != (n = input.read(buffer))) 
         {
             output.write(buffer, 0, n);
             count += n;
         }
         output.flush();
         return count;
     }


    @SuppressLint("SdCardPath")
    private void copyServerDatabase(Context context) 
    {
            BtcDb db = new BtcDb(context,"clean.db",null,0);
            // by calling this line an empty database will be created into the default system path
            // of this app - we will then overwrite this with the database from the server
            db.getReadableDatabase();
            db.close();


            OutputStream os = null;
            InputStream is = null;
            try {
                  // Log.d("BTC", "Copying DB from server version into app");
                    is = context.openFileInput("basesqlite.db");
                    os = new FileOutputStream("/data/data/com.example.btc_pe/databases/"); 

                    copyFile(os, is);
            } 
            catch (Exception e) 
            {
                    Log.e("BTC", "Server Database was not found - did it download correctly?", e);                          
            } 
            finally 
            {
                    try 
                    {
                            //Close the streams
                            if(os != null)
                            {
                                    os.close();
                            }
                            if(is != null)
                            {
                                    is.close();
                            }
                    } 
                    catch (IOException e) 
                    {
                            Log.e("BTC", "failed to close databases");
                    }
            }
            Log.d("BTC", "Done Copying DB from server");
      }




     private static void copyFile(OutputStream os, InputStream is) throws IOException 
     {
            byte[] buffer = new byte[1024];
            int length;
            while((length = is.read(buffer))>0)
            {
                    os.write(buffer, 0, length);
            }
            os.flush();
    }
}
@SuppressLint(“SdCardPath”)
公共最终类DownloadHelper扩展异步任务
{
语境;
File cheminBdd=新文件(“/data/data/com.example.btc_pe/databases/basesqlite.db”);
公共下载助手(上下文ctxt)
{this.context=ctxt;}
@凌驾
受保护的Void doInBackground(Void…参数)
{
//TODO自动生成的方法存根
尝试
{
下载数据库(cheminBdd);
//copyServerDatabase(this.context);
} 
捕获(例外情况除外)
{
Log.e(“BTC”,“未能下载数据库!”,例如);
}
返回null;
}
私有静态void downloadDatabase(文件destFile)引发IOException
{        
URLCON;
InputStream=null;
OutputStream os=null;
尝试
{
日志d(“BTC”、“开始DL”);
URL=新URL(“adresse”+“basesqlite.db”);
ucon=url.openConnection();
日志d(“BTC”,“连接打开”);
is=ucon.getInputStream();
Log.d(“BTC”,“流入got”);
os=新文件输出流(destFile);
Log.d(“BTC”,“首件副本()”);
副本(is、os);
日志d(“BTC”,“结束DL”);
} 
最后
{
如果(os!=null)尝试{os.close();}catch(Exception ex){Log.e(“BTC”,“无法正常关闭输出流”,ex);}
如果(is!=null),请尝试{is.close();}catch(Exception ex){Log.e(“BTC”,“无法正常关闭输入流”,ex);}
} 
}
公共静态int复制(InputStream输入、OutputStream输出)引发IOException
{
字节[]缓冲区=新字节[8192];
整数计数=0;
int n=0;
而(-1!=(n=input.read(buffer)))
{
输出写入(缓冲区,0,n);
计数+=n;
}
output.flush();
返回计数;
}
@SuppressLint(“SdCardPath”)
私有void copyServerDatabase(上下文)
{
BtcDb db=新的BtcDb(上下文“clean.db”,null,0);
//通过调用此行,将在默认系统路径中创建一个空数据库
//此应用程序的名称-然后我们将使用服务器上的数据库覆盖此名称
db.getReadableDatabase();
db.close();
OutputStream os=null;
InputStream=null;
试一试{
//Log.d(“BTC”,“将数据库从服务器版本复制到应用程序”);
is=context.openFileInput(“basesqlite.db”);
os=新文件输出流(“/data/data/com.example.btc\u pe/databases/”);
复制文件(os,is);
} 
捕获(例外e)
{
e(“BTC”,“未找到服务器数据库-是否正确下载?”,e);
} 
最后
{
尝试
{
//关闭溪流
如果(os!=null)
{
os.close();
}
如果(is!=null)
{
is.close();
}
} 
捕获(IOE异常)
{
Log.e(“BTC”,“无法关闭数据库”);
}
}
Log.d(“BTC”,“完成从服务器复制数据库”);
}
私有静态void copyFile(OutputStream os,InputStream is)引发IOException
{
字节[]缓冲区=新字节[1024];
整数长度;
而((长度=is.read(缓冲区))>0)
{
写操作(缓冲区,0,长度);
}
os.flush();
}
}
我通过Actionbar按钮调用更新,使用实例对象中的DownloadHelper.execute()方法。 然后,在传递“is=ucon.getInputStream();”之后,我得到一个异常,我得到以下日志:

当然,我在服务器上检查了这个文件,我的URL,我的Android设备。无事可做

如果有人能帮我一把,我就迷路了:/