我正在尝试将 google 任务与我的应用程序同步。为此,我创建了一个类,其中创建了所有需要获取任务列表和创建列表等的方法。
现在我想测试这些方法是否有效。为此,我创建了一个扩展AsyncTask
.
我必须在这个任务中传递一些参数,就像在我有参数的方法中一样。就像在getTaskList(Strig listId)
. 要从任何列表中获取任务,我需要提供列表的 id。
现在,当我尝试在 AsyncTask 中传递参数时。在调用它时
result = gTaskSyncer.getTaskList(params[0].toString());
它在这里扔一个ArrayIndexOutOfBoundException
。
测试异步任务
public class TestAsyncTask extends AsyncTask<Object,Void,List<Task>>{
private com.google.api.services.tasks.Tasks mService = null;
private Exception mLastError = null;
private MainActivity activity;
private com.google.api.services.tasks.Tasks client = null;
public TestAsyncTask(GoogleAccountCredential credential, MainActivity activity) {
HttpTransport transport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
mService = new com.google.api.services.tasks.Tasks.Builder(
transport, jsonFactory, credential)
.setApplicationName("Google Tasks API Android Quickstart")
.build();
this.activity = activity;
}
protected List<Task> doInBackground(Object... params) {
GTaskSyncer gTaskSyncer = new GTaskSyncer(activity);
List<Task> result = new ArrayList<>();
try {
result = gTaskSyncer.getTaskList(params[0].toString());
} catch (IOException e) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, "No tasks.", Toast.LENGTH_SHORT).show();
}
});
}
return result;
}
@Override
protected void onPostExecute(List<Task> output) {
activity.mProgress.hide();
if (output == null || output.size() == 0) {
activity.mOutputText.setText("No results returned.");
} else {
activity.mOutputText.setText(TextUtils.join("\n", output));
}
}
}
GTaskSyncer
public class GTaskSyncer
{
final MainActivity activity;
final com.google.api.services.tasks.Tasks mService;
private Exception mLastError = null;
GTaskSyncer(MainActivity activity) {
this.activity = activity;
mService = activity.mService;
}
public List<TaskList> getAllTaskList() throws IOException
{
List<TaskList> result = new ArrayList<TaskList>();
TaskLists taskLists = mService.tasklists().list().execute();
for (TaskList taskList : taskLists.getItems()) {
result.add(taskList);
}
return result;
}
public TaskList createList() throws IOException
{
TaskList taskList = new TaskList();
taskList = activity.mService.tasklists().insert(taskList).execute();
return taskList;
}
public Task createTask(String listId) throws IOException
{
Task task = new Task();
task = activity.mService.tasks().insert(listId, task).execute();
return task;
}
public Task getTask(String listId,String taskId) throws IOException
{
Task task = activity.mService.tasks().get(listId, taskId).execute();
return task;
}
public List<Task> getTaskList(String listId) throws IOException {
List<Task> result = new ArrayList<Task>();
List<Task> tasks = mService.tasks().list(listId).execute().getItems();
if (tasks != null) {
for (Task task : tasks) {
result.add(task);
}
} else {
Toast.makeText(activity, "No tasks.", Toast.LENGTH_SHORT).show();
}
return result;
}
}
另外我该如何测试呢?当我们调用它时如何为方法提供一个listId?