【问题标题】:IllegalArgumentException: Trying to execute a remote operation asynchronously without a handler to the listener's threadIllegalArgumentException:尝试在没有侦听器线程处理程序的情况下异步执行远程操作
【发布时间】:2015-09-19 15:30:08
【问题描述】:

我想在服务中上传

我正在使用这个代码示例库来上传 owncloud.org

public class MainActivity extends Activity implements OnRemoteOperationListener, OnDatatransferProgressListener {`
private Handler mHandler;     
private OwnCloudClient mClient; `
   private void startUpload() {
    File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
    File fileToUpload = ; 
    String remotePath =; 
    String mimeType =;
    UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(fileToUpload.getAbsolutePath(), remotePath, mimeType);
    uploadOperation.execute(mClient, this, mHandler);
}

我想在服务端上传我这样写:

公共类 MyService 扩展服务实现 OnRemoteOperationListener { 私有处理程序 mHandler;

private OwnCloudClient mClient;

public MyService() {
}

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

}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // Let it continue running until it is stopped.


    if (intent != null) {
        final String action = intent.getAction();

        if (ACTION.equals(action)) {



            Uri serverUri = Uri.parse(server);
            mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true);
            mClient.setCredentials(
                    OwnCloudCredentialsFactory.newBasicCredentials(
                            username, pass
                    )
            );


            try {
                actionSendList(dest);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }  return START_STICKY;}




private void actionSendList(String dest) throws IOException {


        //String remotePath = FileUtils.PATH_SEPARATOR +"/nuovo/"+ files.gString mimeType = getString(R.string.sample_file_mimetype);
        UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(files.get(i).getAbsolutePath(), remotePath , null);
        //uploadOperation.addDatatransferProgressListener(context);
        uploadOperation.execute(mClient,this,mHandler);


    }


}

    @Override
    public void onRemoteOperationFinish (RemoteOperation caller, RemoteOperationResult result){
        if (!result.isSuccess()) {
            Toast.makeText(this, R.string.todo_operation_finished_in_fail, Toast.LENGTH_SHORT).show();
            Log.i("service", "fail");
        }
    }

} uploadOperation.execute(mClient,this,mHandler);

【问题讨论】:

    标签: java android owncloud


    【解决方案1】:

    您正在使用意图服务。 onHandleIntent 方法将在后台线程上执行,您尝试从错误的后台线程执行 AsyncTask。 AsyncTask 必须从主线程执行。主线程将有一个与之关联的默认处理程序。你必须使用Android的Service组件而不是IntentService。

    【讨论】:

    • 我还没有解决。我在服务 android 中创建了处理程序并调用 uploadOperation.execute(mClient,this,mHandler);在启动命令中但 l'app 崩溃
    • 我忘记了 mHandler = new Handler();
    猜你喜欢
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 2014-03-18
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    相关资源
    最近更新 更多