APP每次调用ftp写操作时都会崩溃。
我在Fragment
中有以下两种方法。
public void uploadData () {
String domain = "10.142.42.10"; // <<< will it work as it is private ip?
String user = "xxxx";
String password = "xxxx";
String serverRoad = "Documents";
File file = new File("/sdcard/bluetooth/NRC_jiten.pdf");
FTPClient ftp = new FTPClient();
try {
ftp.connect(InetAddress.getByName(domain));
//ftp.connect(domain,21);
ftp.login(user, password);
ftp.changeWorkingDirectory(serverRoad);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
FileInputStream is = new FileInputStream(file);
BufferedInputStream buffIn = new BufferedInputStream(is);
ftp.enterLocalPassiveMode();
ftp.storeFile("jiten_pdf.pdf", buffIn);
buffIn.close();
ftp.logout();
ftp.disconnect();
} catch (Exception e) {
Toast.makeText(getActivity(), "err:" + e,
Toast.LENGTH_SHORT).show();
}
}
public void uploadFileBackground () {
new Thread(new Runnable() {
public void run() {
uploadData();
}
}).start();
}
现在,从Activity
我点击按钮后触发uploadFileBackground
。
代码有什么问题吗?我已经推荐过这个网站,但无法成功。
TIA。
logcat的
08-25 19:00:56.042 16119-16119/com.sbordolo.potholefinder W/GooglePlayServicesUtil﹕ Google Play services out of date. Requires 7571000 but found 6774470
08-25 19:00:56.044 16119-16119/com.sbordolo.potholefinder W/GooglePlayServicesUtil﹕ Google Play services out of date. Requires 7571000 but found 6774470
08-25 19:00:56.047 16119-16119/com.sbordolo.potholefinder W/GooglePlayServicesUtil﹕ Google Play services out of date. Requires 7571000 but found 6774470
08-25 19:00:56.047 16119-16119/com.sbordolo.potholefinder W/GooglePlayServicesUtil﹕ Google Play services out of date. Requires 7571000 but found 6774470
08-25 19:00:56.049 16119-16119/com.sbordolo.potholefinder W/GooglePlayServicesUtil﹕ Google Play services out of date. Requires 7571000 but found 6774470
08-25 19:00:56.050 16119-16119/com.sbordolo.potholefinder W/GooglePlayServicesUtil﹕ Google Play services out of date. Requires 7571000 but found 6774470
08-25 19:00:56.051 16119-16119/com.sbordolo.potholefinder W/GooglePlayServicesUtil﹕ Google Play services out of date. Requires 7571000 but found 6774470
08-25 19:00:56.052 16119-16119/com.sbordolo.potholefinder W/GooglePlayServicesUtil﹕ Google Play services out of date. Requires 7571000 but found 6774470
08-25 19:00:56.052 16119-16119/com.sbordolo.potholefinder W/GooglePlayServicesUtil﹕ Google Play services out of date. Requires 7571000 but found 6774470
08-25 19:00:56.056 16119-16119/com.sbordolo.potholefinder W/GooglePlayServicesUtil﹕ Google Play services out of date. Requires 7571000 but found 6774470
08-25 19:00:56.056 16119-16119/com.sbordolo.potholefinder W/GooglePlayServicesUtil﹕ Google Play services out of date. Requires 7571000 but found 6774470
08-25 19:00:56.058 16119-16119/com.sbordolo.potholefinder W/GooglePlayServicesUtil﹕ Google Play services out of date. Requires 7571000 but found 6774470
08-25 19:00:56.079 16119-16138/com.sbordolo.potholefinder D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
08-25 19:00:56.080 16119-16119/com.sbordolo.potholefinder D/﹕ HostConnection::get() New Host Connection established 0xb3ffc140, tid 16119
08-25 19:00:56.088 16119-16119/com.sbordolo.potholefinder D/Atlas﹕ Validating map...
08-25 19:00:56.474 16119-16138/com.sbordolo.potholefinder D/﹕ HostConnection::get() New Host Connection established 0xb3ffc3c0, tid 16138
08-25 19:00:56.483 16119-16138/com.sbordolo.potholefinder I/OpenGLRenderer﹕ Initialized EGL, version 1.4
08-25 19:00:56.488 16119-16138/com.sbordolo.potholefinder D/OpenGLRenderer﹕ Enabling debug mode 0
08-25 19:00:56.514 16119-16138/com.sbordolo.potholefinder W/EGL_emulation﹕ eglSurfaceAttrib not implemented
08-25 19:00:56.514 16119-16138/com.sbordolo.potholefinder W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xb3fdb4e0, error=EGL_SUCCESS
08-25 19:00:56.844 16119-16119/com.sbordolo.potholefinder E/WindowManager﹕ android.view.WindowLeaked: Activity com.sbordolo.potholefinder.HomeScreen has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{9052cc6 V.E..... R....... 0,0-1026,591} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:271)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:298)
at com.sbordolo.potholefinder.CoordinateClass.isGooglePlayServicesAvailable(CoordinateClass.java:110)
at com.sbordolo.potholefinder.CoordinateClass.onActivityCreated(CoordinateClass.java:66)
at android.app.Fragment.performActivityCreated(Fragment.java:2061)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:912)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1049)
at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1869)
at android.app.Activity.performCreateCommon(Activity.java:5985)
at android.app.Activity.performCreate(Activity.java:5992)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:0)
首先,使用Google Play应用更新您的Google Play服务。正如你在这里看到的那样:
Google Play services out of date.
它已经过时了。
其次,请不要像这样使用Thread
在Android上执行背景操作
(...)它违反了单线程模型的第二条规则: 没有 从UI线程外部访问Android UI工具包 。
尝试使用AsyncTask
,Service
或Handler
。
有关线程here,服务here和处理程序here的更多信息。
现在针对您的问题,您将收到此异常:
android.view.WindowLeaked: Activity com.sbordolo.potholefinder.HomeScreen has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{9052cc6 V.E..... R....... 0,0-1026,591} that was originally added here
由于代码中没有被忽略WindowLeaked
,因此最常抛出Dialog
个例外。
您的logcat跟踪显示Dialog正在尝试弹出:
at android.app.Dialog.show(Dialog.java:298)
编辑:因为Toast
而崩溃了。删除它,或尝试上面的解决方案,你应该没事。
第二次编辑:正如您所提到的,只是为了让答案完整,您的ftp服务器需要设置正确的权限,因此您的程序可以在那里编写内容。您的chmod 777
可以正常工作,但要小心,因为它让您的服务器处于打开状态,以便任何人都可以在那里写信。
答案 1 :(得分:0)
我找到了根本原因,在查看pcap文件时发现了一个权限问题。 FTP登录成功,但是由于权限问题,客户端试图放置文件时,它失败了!将目录权限设为777,已修复!
感谢Mauker帮助我解决这个问题。
感谢。
答案 2 :(得分:0)
这是我为应用程序实现的代码。
public static String getDiretorioBackup() {
String diretorio = "";
if (android.os.Build.VERSION.SDK_INT >= 23) {
diretorio = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/" + "dir";
}
else {
diretorio = Environment.getExternalStorageDirectory() + "/" + "dir";
}
return diretorio;
}
public static FTPClient connnectFTP(String ip, String userName, String pass) throws Exception{
FTPClient mFtpClient = new FTPClient();
mFtpClient.setConnectTimeout(10 * 1000);
mFtpClient.connect( ip );
boolean status = mFtpClient.login(userName, pass);
Log.e("login ftp:", String.valueOf(status));
return mFtpClient;
}
public static Boolean uploadFile(String email, String pathUpload, String guidStr) throws Exception
{
FTPClient ftpClient = connnectFTP(Constantes.FTP, Constantes.FTP_USER, Constantes.FTP_PASS);
if(ftpClient != null)
{
FileInputStream srcFileStream = new FileInputStream( getDiretorioBackup() +"/"+ ConstantesBanco.DATABASE_NAME ); //antes de fazer o ftp é feita a cópia local
ftpClient.enterLocalPassiveMode();
ftpClient.setFileTransferMode(ftpClient.BINARY_FILE_TYPE);
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
boolean status = ftpClient.storeFile( pathUpload+fileName , srcFileStream);
Log.d(TAG, String.valueOf(status) + "- reply code:"+ ftpClient.getReplyCode()+" "+ftpClient.getReplyString());
srcFileStream.close();
ftpClient.disconnect();
return status;
}
else{
throw new BackupFtpException(Constantes.ERRO_SERV_FTP);
}
}