IllegalArgumentException:尝试在没有侦听器线程处理程序的情况下异步执行远程操作
IllegalArgumentException: Trying to execute a remote operation asynchronously without a handler to the listener's thread
我要在服务中上传
我正在使用此代码示例库进行上传 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);
}
我想在服务端上传我这样写:
public class MyService extends Service implements 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);
您正在使用意向服务。 onHandleIntent 方法将在后台线程上执行,而您正试图从错误的后台线程执行 AsyncTask。 AsyncTask 必须从主线程执行。主线程将有一个与之关联的默认处理程序。您必须使用 Android 的服务组件而不是 IntentService。
我要在服务中上传
我正在使用此代码示例库进行上传 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);
}
我想在服务端上传我这样写:
public class MyService extends Service implements 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);
您正在使用意向服务。 onHandleIntent 方法将在后台线程上执行,而您正试图从错误的后台线程执行 AsyncTask。 AsyncTask 必须从主线程执行。主线程将有一个与之关联的默认处理程序。您必须使用 Android 的服务组件而不是 IntentService。