0

我正在下载 task-android-sample( http://code.google.com/p/google-api-java-client/issues/detail?id=479 )。但是当我在我的安卓设备上运行这个例子时,我得到了错误

错误出现在此函数中

protected void doInBackground() throws IOException {
Log.d(Tag, "doInBackground");
List<String> result = new ArrayList<String>();
List<Task> tasks =
    client.tasks().list("@default").setFields("items/title").execute().getItems();
Log.d(Tag, "трассировка");
if (tasks != null) {
  for (Task task : tasks) {
    result.add(task.getTitle());
  }
} else {
  result.add("No tasks.");
}
activity.tasksList = result;

}

描述客户端

final com.google.api.services.tasks.Tasks client;
client = activity.service;

可能是什么问题?我是新手,请帮忙。

04-23 08:55:06.789: E/TasksSample(3778):   com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
04-23 08:55:06.789: E/TasksSample(3778): {
04-23 08:55:06.789: E/TasksSample(3778):   "code": 403,
04-23 08:55:06.789: E/TasksSample(3778):   "errors": [
04-23 08:55:06.789: E/TasksSample(3778):     {
04-23 08:55:06.789: E/TasksSample(3778):       "domain": "usageLimits",
04-23 08:55:06.789: E/TasksSample(3778):       "message": "Access Not Configured",
04-23 08:55:06.789: E/TasksSample(3778):       "reason": "accessNotConfigured"
04-23 08:55:06.789: E/TasksSample(3778):     }
04-23 08:55:06.789: E/TasksSample(3778):   ],
04-23 08:55:06.789: E/TasksSample(3778):   "message": "Access Not Configured"
4

2 回答 2

0
 public static final String KEY = null;

确保您添加了 key 代替 null inClientCredentials.java

于 2013-04-23T07:00:24.907 回答
0

异步加载任务

package com.google.api.services.samples.tasks.android;

import com.google.api.services.tasks.model.Task;
import android.util.Log;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


class AsyncLoadTasks extends CommonAsyncTask {
  final String Tag="States";
  AsyncLoadTasks(TasksSample tasksSample) {
    super(tasksSample);
  }

  @Override
  protected void doInBackground() throws IOException {
    Log.d(Tag, "doInBackground");
    List<String> result = new ArrayList<String>();
    List<Task> tasks =
    client.tasks().list("@default").setFields("items/title").execute().getItems();
    Log.d(Tag, "трассировка");
    if (tasks != null) {
  for (Task task : tasks) {
    result.add(task.getTitle());
  }
} else {
  result.add("No tasks.");
}
activity.tasksList = result;

  }

  static void run(TasksSample tasksSample) {
    new AsyncLoadTasks(tasksSample).execute();
  }
}

通用异步任务

package com.google.api.services.samples.tasks.android;

import     com.google.api.client.googleapis.extensions.android.gms.auth.GooglePlayServicesAvailabilityI    OException;
import     com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException;

import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import java.io.IOException;


abstract class CommonAsyncTask extends AsyncTask<Void, Void, Boolean> {
  final String Tag="States";
  final TasksSample activity;
  final com.google.api.services.tasks.Tasks client;
  private final View progressBar;

  CommonAsyncTask(TasksSample activity) {
    Log.d(Tag, "CommonAsyncTask");
    this.activity = activity;
    client = activity.service;
    progressBar = activity.findViewById(R.id.title_refresh_progress);
  }

  @Override
  protected void onPreExecute() {
    Log.d(Tag, "onPreExecute");
    super.onPreExecute();
    activity.numAsyncTasks++;
    progressBar.setVisibility(View.VISIBLE);
  }

  @Override
  protected final Boolean doInBackground(Void... ignored) {
    Log.d(Tag, "Boolean doInBackground");
    try {
      doInBackground();
      return true;
        } catch (final GooglePlayServicesAvailabilityIOException availabilityException)     {
      Log.d(Tag, "1");
      activity.showGooglePlayServicesAvailabilityErrorDialog(
      availabilityException.getConnectionStatusCode());
    } catch (UserRecoverableAuthIOException userRecoverableException) {
  Log.d(Tag, "2");
  activity.startActivityForResult(
      userRecoverableException.getIntent(), TasksSample.REQUEST_AUTHORIZATION);
    } catch (IOException e) {
  Log.d(Tag, "3");
  Utils.logAndShow(activity, TasksSample.TAG, e);
    }
    return false;
  }

  @Override
  protected final void onPostExecute(Boolean success) {
Log.d(Tag, "onPostExecute");
super.onPostExecute(success);
if (0 == --activity.numAsyncTasks) {
  progressBar.setVisibility(View.GONE);
}
if (success) {
  activity.refreshView();
    }
  }

  abstract protected void doInBackground() throws IOException;
}

任务示例

package com.google.api.services.samples.tasks.android;


import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.tasks.TasksScopes;

import android.accounts.AccountManager;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;



public final class TasksSample extends Activity {


  private static final Level LOGGING_LEVEL = Level.OFF;

  private static final String PREF_ACCOUNT_NAME = "accountName";

  static final String TAG = "TasksSample";

  static final int REQUEST_GOOGLE_PLAY_SERVICES = 0;

  static final int REQUEST_AUTHORIZATION = 1;

  static final int REQUEST_ACCOUNT_PICKER = 2;

  final HttpTransport transport = AndroidHttp.newCompatibleTransport();

  final JsonFactory jsonFactory = new GsonFactory();

  GoogleAccountCredential credential;

  List<String> tasksList;

  ArrayAdapter<String> adapter;

  com.google.api.services.tasks.Tasks service;

  int numAsyncTasks;

  private ListView listView;
  final String Tag="States";

  @Override
  public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
Log.d(Tag, "onCreate");
// enable logging
Logger.getLogger("com.google.api.client").setLevel(LOGGING_LEVEL);
// view and menu
setContentView(R.layout.calendarlist);
listView = (ListView) findViewById(R.id.list);
// Google Accounts
credential = GoogleAccountCredential.usingOAuth2(this, TasksScopes.TASKS);
SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
credential.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));
// Tasks client
service =
    new com.google.api.services.tasks.Tasks.Builder(transport, jsonFactory, credential)
        .setApplicationName("Google-TasksAndroidSample/1.0").build();
  }

  void showGooglePlayServicesAvailabilityErrorDialog(final int connectionStatusCode) {
runOnUiThread(new Runnable() {
  public void run() {
    Log.d(Tag, "run");
    Dialog dialog =
        GooglePlayServicesUtil.getErrorDialog(connectionStatusCode, TasksSample.this,
            REQUEST_GOOGLE_PLAY_SERVICES);
    dialog.show();
  }
    });
  }

  void refreshView() {
Log.d(Tag, "refreshView");
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tasksList);
listView.setAdapter(adapter);
  }

  @Override
  protected void onResume() {
Log.d(Tag, "onResume");
super.onResume();
if (checkGooglePlayServicesAvailable()) {
  haveGooglePlayServices();
}
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(Tag, "onActivityResult");
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
  case REQUEST_GOOGLE_PLAY_SERVICES:
    if (resultCode == Activity.RESULT_OK) {
      haveGooglePlayServices();
    } else {
      checkGooglePlayServicesAvailable();
    }
    break;
  case REQUEST_AUTHORIZATION:
    if (resultCode == Activity.RESULT_OK) {
      AsyncLoadTasks.run(this);
    } else {
      chooseAccount();
    }
    break;
  case REQUEST_ACCOUNT_PICKER:
    if (resultCode == Activity.RESULT_OK && data != null && data.getExtras() != null) {
      String accountName = data.getExtras().getString(AccountManager.KEY_ACCOUNT_NAME);
      if (accountName != null) {
        credential.setSelectedAccountName(accountName);
        SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(PREF_ACCOUNT_NAME, accountName);
        editor.commit();
        AsyncLoadTasks.run(this);
      }
    }
    break;
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
Log.d(Tag, "onCreateOptionsMenu");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
Log.d(Tag, "onOptionsItemSelected");
switch (item.getItemId()) {
  case R.id.menu_refresh:
    AsyncLoadTasks.run(this);
    break;
  case R.id.menu_accounts:
    chooseAccount();
    return true;
}
return super.onOptionsItemSelected(item);
  }


  private boolean checkGooglePlayServicesAvailable() {
Log.d(Tag, "checkGooglePlayServicesAvailable");
final int connectionStatusCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (GooglePlayServicesUtil.isUserRecoverableError(connectionStatusCode)) {
  showGooglePlayServicesAvailabilityErrorDialog(connectionStatusCode);
  return false;
}
return true;
  }

  private void haveGooglePlayServices() {
Log.d(Tag, "haveGooglePlayServices");
// check if there is already an account selected
if (credential.getSelectedAccountName() == null) {
  Log.d(Tag, "user to choose account");
  // ask user to choose account
  chooseAccount();
} else {
  Log.d(Tag, "load calendars");
  // load calendars
  AsyncLoadTasks.run(this);
}
  }

  private void chooseAccount() {
Log.d(Tag, "chooseAccount");
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
  }

}

实用程序

package com.google.api.services.samples.tasks.android;

import com.google.android.gms.auth.GoogleAuthException;
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;

import android.app.Activity;
import android.content.res.Resources;
import android.util.Log;
import android.widget.Toast;

public class Utils {

  public static void logAndShow(Activity activity, String tag, Throwable t) {
 //   Log.d(Tag, "onPostExecute");
Log.e(tag, "Error", t);
String message = t.getMessage();
if (t instanceof GoogleJsonResponseException) {
  GoogleJsonError details = ((GoogleJsonResponseException) t).getDetails();
  if (details != null) {
    message = details.getMessage();
  }
} else if (t.getCause() instanceof GoogleAuthException) {
  message = ((GoogleAuthException) t.getCause()).getMessage();
}
showError(activity, message);
  }

/**

  public static void logAndShowError(Activity activity, String tag, String message) {
String errorMessage = getErrorMessage(activity, message);
Log.e(tag, errorMessage);
showErrorInternal(activity, errorMessage);
  }


  public static void showError(Activity activity, String message) {
String errorMessage = getErrorMessage(activity, message);
showErrorInternal(activity, errorMessage);
  }

  private static void showErrorInternal(final Activity activity, final String errorMessage) {
activity.runOnUiThread(new Runnable() {
  public void run() {
    Toast.makeText(activity, errorMessage, Toast.LENGTH_LONG).show();
  }
    });
  }

  private static String getErrorMessage(Activity activity, String message) {
Resources resources = activity.getResources();
if (message == null) {
  return resources.getString(R.string.error);
}
return resources.getString(R.string.error_format, message);
  }
}

这是我的代码。我能解决什么问题?

于 2013-04-23T07:28:09.733 回答