异步任务FTP上传Android服务崩溃?

时间:2015-01-13 16:40:03

标签: java android service android-asynctask ftp-client

我正在尝试通过FTP上传设置目录中的文件。该文件名为" advancedsettings.xml"。

代码在活动的异步任务中使用时工作正常,但在尝试使用服务在异步任务中执行完全相同的操作时,应用程序崩溃时出现以下错误(一个或多个):

  

01-13 16:21:08.403 21134-21151 / com.name.example.xx E / AndroidRuntime:   致命异常:AsyncTask#1进程:com.name.example.xx,PID:21134   java.lang.RuntimeException:执行时发生错误   doInBackground()在android.os.AsyncTask $ 3.done(AsyncTask.java:300)at at   java.util.concurrent.FutureTask.finishCompletion(F utureTask.java:355)   at java.util.concurrent.FutureTask.setException(Futur eTask.java:222)   在java.util.concurrent.FutureTask.run(FutureTask.jav a:242)at at   android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTas k.java:231)at at   java.util.concurrent.ThreadPoolExecutor.runWorker(   ThreadPoolExecutor.java:1112)at   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run   (ThreadPoolExecutor.java:587)在java.lang.Thread.run(Thread.java:841)   引起:java.lang.RuntimeException:无法在里面创建处理程序   没有调用Looper.prepare()的线程   android.os.Handler。(Handler.java:200)at   android.os.Handler。(Handler.java:114)at   android.widget.Toast $ TN。(Toast.java:388)at   android.widget.Toast。(Toast.java:114)at   android.widget.Toast.makeText(Toast.java:273)at   com.name.example.xx.MyService $ FTPUpload.doInBackgr   ound(MyService.java:58)at   com.name.example.xx.MyService $ FTPUpload.doInBackgr   ound(MyService.java:55)at   android.os.AsyncTask $ 2.call(AsyncTask.java:288)at   java.util.concurrent.FutureTask.run(FutureTask.jav a:237)   ************在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTas k.java:231)   ************ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)   ************ at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)   ************ at java.lang.Thread.run(Thread.java:841)01-13 16:21:08.473 862-862 /? W / ContextImpl:调用系统中的方法   没有合格用户的流程:   android.app.ContextImpl.sendBroadcast:1505   com.android.server.analytics.data.collection.appli   cation.CrashAnrDetector.broadcastEvent:296   com.android.server.analytics.data.collection.appli   cation.CrashAnrDetector.processDropBoxEntry:254   com.android.server.analytics.data.collection.appli   cation.CrashAnrDetector.access $ 100:60   com.android.server.analytics.data.collection.appli   cation.CrashAnrDetector $ 1.onReceive:102 01-13 16:21:08.713 862-21154 /?   E / android.os.Debug:!@ Rollstate> sdumpstate -k -t -z -d -o   /数据/日志/ dumpstate_app_error

以下是我在主要活动中点击服务的方式(按钮点击):

Intent inetnt=new Intent(FileChooser.this, MyService.class);
        startService(inetnt);

这是服务代码:

public class MyService extends Service {

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    Toast.makeText(MyService.this, "service start", Toast.LENGTH_LONG).show();
    new FTPUpload().execute();

}

@Override
public void onDestroy() {
    super.onDestroy();


}

@Override
public void onLowMemory() {
    super.onLowMemory();

}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    return super.onStartCommand(intent, flags, startId);

}

public class FTPUpload extends AsyncTask<Void, Void, Void> {
    protected Void doInBackground(Void...params) {

        Toast.makeText(MyService.this, "Service FTP Upload Start", Toast.LENGTH_LONG).show();

        FTPClient con = null;

        try

        {
            con = new FTPClient();
            con.connect("host");

            // Check your USERNAME e.g myuser@mywebspace.com and check your PASSWORD to ensure they are OK.
            if (con.login("username", "password")) {
                con.enterLocalPassiveMode(); // important!
                con.setFileType(FTP.BINARY_FILE_TYPE);

                Random rand = new Random();
                String randomIntString = String.valueOf(rand.nextInt()).replaceAll("-", "");


                FileInputStream in = new FileInputStream("/storage/emulated/0/");
                //con.makeDirectory("/" + randomIntString + "/");
                con.storeFile("advancedsettings.xml", in);
                in.close();


                //PASS URL TO NEXT ACTIVITY AND LAUNCH NEXT ACTIVITY ON NOTIFICATION CLICK


            } else {


            }
        } catch (
                Exception e
                )

        { //if file does not upload successfully, write an error log.
            Toast.makeText(MyService.this, "An error occured, please report: " + e, Toast.LENGTH_LONG).show();
        }

        Toast.makeText(MyService.this, "Service FTP Upload Complete", Toast.LENGTH_LONG).show();

        return null;
    }

}

}

我可以确认我已在应用程序代码中的Android Manifest文件中声明了该服务,如下所示:

<service android:name=".MyService"/>

任何想法的家伙?非常感谢您的帮助!我非常希望能够深究这一点。

ķ

2 个答案:

答案 0 :(得分:2)

下面

Toast.makeText(MyService.this, "Service FTP Upload Start", Toast.LENGTH_LONG).show();

您正试图Toast来自doInBackground(来自其他主题)。在开始onPreExecute执行

之前,使用doInBackground显示Toast

答案 1 :(得分:2)

请不要访问doInBackground中的主要主题,例如运行Toast。请使用onPreExecute代替此类操作。

Toast.makeText(MyService.this, 
               "Service FTP Upload Start", 
               Toast.LENGTH_LONG).show();

doInBackground应该/不能访问UI线程。对于您的完整任务Toasts尝试从doInBackground返回一个值,您将获得onPostExecute中可以显示祝酒词的返回值

有关详细信息,请查看文档: Documentation