имам този код във фонов режим в клас Async:
FileInputStream InputStream = new FileInputStream(new File(DBToUploadFromInternalStorage.getPath()));
if(x!=2){
try{
con.storeFile("/"+Email+"/PilotLogbookDB.itb", InputStream);
bytesAvailable = InputStream.available();
bytesRead = InputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
progress+=bytesRead;
publishProgress((int)((progress*100)/(DBToUploadFromInternalStorage.getPath().length())));
}//end of while statement
publishProgress(100);
}
catch(Exception e){
e.printStackTrace();
x=2;
}
}
InputStream.close();
if(x !=2){
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new
File(getFilesDir()+File.separator+"Cloud Version.itb")));
bufferedWriter.write(String.valueOf(Version));
bufferedWriter.close();
FileInputStream InputStream_ = new FileInputStream(new File(CloudVersionSysForRestore.getPath()));
if(x!=2){
try{
con.storeFile("/"+Email+"/Cloud Version.itb", InputStream_);
}
catch(Exception e){
e.printStackTrace();
x=2;
}
}
InputStream_.close();
}
Проблемът е, че файлът се качва, но лентата за напредъка остава на 0. След качване на файл, при изпълнение след изпълнение, диалоговият прозорец се отхвърля, така че работи
Също така имам грешка в bytesRead = InputStream.read(buffer, 0, bufferSize);
in Logcat
Изход от LogCat:
08-02 02:36:32.884: W/System.err(27519): java.lang.NullPointerException
08-02 02:36:32.894: W/System.err(27519): at libcore.io.IoBridge.read(IoBridge.java:427)
08-02 02:36:32.894: W/System.err(27519): at java.io.FileInputStream.read(FileInputStream.java:179)
08-02 02:36:32.904: W/System.err(27519): at com.itbstudios.pilotlogbook.PilotLogbook_main$AsyncSenderSync.doInBackground(PilotLogbook_main.java:7061)
08-02 02:36:32.904: W/System.err(27519): at com.itbstudios.pilotlogbook.PilotLogbook_main$AsyncSenderSync.doInBackground(PilotLogbook_main.java:1)
08-02 02:36:32.914: W/System.err(27519): at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-02 02:36:32.914: W/System.err(27519): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-02 02:36:32.924: W/System.err(27519): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-02 02:36:32.924: W/System.err(27519): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-02 02:36:32.934: W/System.err(27519): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-02 02:36:32.934: W/System.err(27519): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-02 02:36:32.934: W/System.err(27519): at java.lang.Thread.run(Thread.java:864)
Опитвам се да качвам на FTP с помощта на Async и Apache Lib. v 3.3
ПЪЛЕН асинхронен клас:
private final class AsyncSenderSync extends AsyncTask<Object, Integer, Integer> {
int bytesRead, bytesAvailable;
int progress=0;
byte[] buffer;
int bufferSize = 512;
File DataDirectory = Environment.getDataDirectory();
String DataPath = "//data//com.itbstudios.pilotlogbook//databases//PilotLogbookDB";
File DBToUploadFromInternalStorage = new File(DataDirectory, DataPath);
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(PilotLogbook_main.this);
pd.setTitle("Syncing Data");
pd.setMessage("Please wait, data is syncing...");
pd.setCancelable(false);
pd.setIndeterminate(false);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setProgress(0);
pd.setMax(100);
pd.incrementProgressBy((int)((progress*100)/(DBToUploadFromInternalStorage.length())));
if(!pd.isShowing())
pd.show();
else
{
pd.cancel();
pd.dismiss();
}
}
@Override
protected Integer doInBackground(Object... arg0) {
int x = 0;
DBSettingsHelper.open();
DBHelper.open();
File DataDirectory = Environment.getDataDirectory();
Cursor cursor = DBSettingsHelper.getEmailbyRowId(1);
cursor.moveToFirst();
Cursor cursor_version = DBSettingsHelper.getCloudVersionbyRowId(1);
cursor_version.moveToFirst();
long Version = DBSettingsHelper.CloudStatus(cursor_version);
String Email = DBSettingsHelper.EmailStatus(cursor);
File DatabaseInternalStorageDirectory = Environment.getDataDirectory();
String FinalRestorePointPathForDB = "//data//com.itbstudios.pilotlogbook//databases//PilotLogbookDB";
File EmptyFileForRestore = new File(DatabaseInternalStorageDirectory, FinalRestorePointPathForDB);
FTPClient con = null;
try
{
con = new FTPClient();
con.setConnectTimeout(300000);
con.setDataTimeout(300000);
con.connect("host",21);
if (con.login("user", "pass"))
{
con.enterLocalPassiveMode(); // important!
con.setFileType(FTP.BINARY_FILE_TYPE);
boolean directoryExists = con.changeWorkingDirectory(Email);
File CloudVersionSysForRestore = new File(getFilesDir()+File.separator+"Cloud Version.itb");
try {
CloudVersionSysForRestore.createNewFile();
OutputStream OutstreamCloud = new FileOutputStream(new File(CloudVersionSysForRestore.getPath()));
boolean resultCloud = con.retrieveFile("/"+Email+"/Cloud Version.itb", OutstreamCloud);
OutstreamCloud.close();
BufferedReader bufferedReader = new BufferedReader(new FileReader(new
File(getFilesDir()+File.separator+"Cloud Version.itb")));
String read;
StringBuilder builder = new StringBuilder("");
while((read = bufferedReader.readLine()) != null){
builder.append(read);
}
bufferedReader.close();
long getintfromstring = Long.parseLong(builder.toString());
if(getintfromstring<Version){
FileInputStream InputStream = new FileInputStream(new File(DBToUploadFromInternalStorage.getPath()));
if(x!=2){
try{
con.storeFile("/"+Email+"/PilotLogbookDB.itb", InputStream);
bytesAvailable = InputStream.available();
bytesRead = InputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
progress+=bytesRead;
publishProgress((int)((progress*100)/(DBToUploadFromInternalStorage.getPath().length())));
}//end of while statement
publishProgress(100);
}
catch(Exception e){
e.printStackTrace();
x=2;
}
}
InputStream.close();
if(x !=2){
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new
File(getFilesDir()+File.separator+"Cloud Version.itb")));
bufferedWriter.write(String.valueOf(Version));
bufferedWriter.close();
FileInputStream InputStream_ = new FileInputStream(new File(CloudVersionSysForRestore.getPath()));
if(x!=2){
try{
con.storeFile("/"+Email+"/Cloud Version.itb", InputStream_);
}
catch(Exception e){
e.printStackTrace();
x=2;
}
}
InputStream_.close();
}
}
if(getintfromstring>Version){
OutputStream OutstreamDatabase = new FileOutputStream(new File(EmptyFileForRestore.getPath()));
if(x!=2){
try{
con.retrieveFile("/"+Email+"/PilotLogbookDB.itb", OutstreamDatabase);
}
catch(Exception e){
e.printStackTrace();
x=2;
}
}
OutstreamDatabase.close();
if(x!=2)
DBSettingsHelper.updateCloudVersion(1,getintfromstring);
}
CloudVersionSysForRestore.delete();
DBSettingsHelper.close();
DBHelper.close();
x = 1;
} catch (Exception e) {
e.printStackTrace();
Log.w("timeout", "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
x=2;
}
}
con.logout();
con.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
Log.w("timeout", "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
x=2;
}
return x;
}
@Override
public void onProgressUpdate(Integer... values)
{
super.onProgressUpdate(values);
pd.setProgress(values[0]);
}
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
if(result == 2)
{
final AlertDialog.Builder builder___ = new AlertDialog.Builder(PilotLogbook_main.this);
builder___.setTitle("Error!");
builder___.setMessage(Html.fromHtml(
"Something went wrong (501)"
+ "<br><br>"
+ "Cloud error please try again. If you'll see this error again please report it at:"
+ "<br>"
+"<b>[email protected]</b>"
))
.setCancelable(false)
.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
pd.cancel();
pd.dismiss();
}
});
final AlertDialog alert___ = builder___.create();
alert___.show();
}
pd.dismiss();
InitialReport();
}
}